jeudi 29 août 2019

In my Angular 7 application, using reactive forms..Change this checkbox value to '1' if checked, change value to null if unchecked

I need to change this checkbox value to '1' if it is checked and null if it is unchecked

<div class="form-check col-md-12">

  <input 
    class="form-check-input" 
    formControlName="parentsSsi" 
    id="parentsSsi" 
    type="checkbox" 
    value="1" 
    (change)="unselectNoneOfTheAbove(); 
    checkedBoxValueIsOne()"
   >

</div>

In my TS file:

  unselectNoneOfTheAbove() {
    this.pfinancialSectionSix.patchValue({
      parentsNoneOfTheAbove: null
    });
  }

  // change value of checked check box to '1' if checked
  checkedBoxValueIsOne() {
    if(this.pfinancialSectionSix.controls.parentsSsi.value === null || 
    !this.pfinancialSectionSix.controls.parentsSsi.value){
      this.pfinancialSectionSix.patchValue({
      parentsSsi: '1'
    })
  }
    if(this.pfinancialSectionSix.controls.parentsSsi.value || 
      this.pfinancialSectionSix.controls.parentsSsi.value !== null) {
        this.pfinancialSectionSix.patchValue({
        parentsSsi: null
      })
    }
    console.log('checkbox value: ', this.pfinancialSectionSix.controls.parentsSsi.value)
  }

Currently, it only stays as null, but should toggle between '1' and null if it is checked or unchecked.




Aucun commentaire:

Enregistrer un commentaire