0

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;
  }
````

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.