Home > OS >  How I can set value before using RemoveAt(i) on Angular Reactive Form Array?
How I can set value before using RemoveAt(i) on Angular Reactive Form Array?

Time:12-29

How I can set value before using RemoveAt(i) on Angular Reactive Form Array?

  removeCustomerPhone(custIndex: number, phoneIndex: number) {
    // this.customerPhones(custIndex).at(phoneIndex).setValue({
    //   'IsDeleted': true
    // })
     this.customerPhones(custIndex).removeAt(phoneIndex, {
      emitEvent  : false
    });
  }

Following is the methods to bind phones:-

customerPhones(custIndex: number): FormArray {
    return this.Addresses()
      .at(custIndex)
      .get('Phones') as FormArray;
  }
 
  newPhone(): FormGroup {
    this.phoneForm = this.formBuilder.group({
      ID:0,
      phone1: '',
      addressId:0,
      IsDeleted:false
    });
    this.phoneForm.valueChanges.subscribe(a=>{
      a.stateEnum = State.Modified
    });
    return this.phoneForm;
  }
````

CodePudding user response:

to give value to a FormGroup you use setValue but you need give one value to each field of the formGroup: `

   this.customerPhones(custIndex).at(phoneIndex)
        .setValue({ID:..,phone1:...,addressId:...,IsDeleted:...})

If you only want to change one property (o severals but not all) you use patchValue

this.customerPhones(custIndex).at(phoneIndex)
            .patchValue({IsDeleted:true})

CodePudding user response:

you can use the patchValue method of the form control to set the value of specific form control properties. For example:

  this.customerPhones(custIndex).at(phoneIndex).patchValue({
  'IsDeleted': true
});
  • Related