MonteCristo MonteCristo - 27 days ago 11
Javascript Question

update number field value in nested angular 2 component

I have a nested/sub formgroup. It has a number field that need to update by clicking a button. I can update the value itself but I can't get the field updated.

How do I fix it?

here is the plnkr
https://plnkr.co/edit/ZN3cGHHRNvx7348x1PSm?p=preview

increment() {
this.number++;
this.adressForm.value.age = this.number++;

}


thanks in advance.

Answer

In AddressComponent change your code to

increment() {
  let currentAgeValue : number = this.adressForm.controls.age.value;
  this.adressForm.controls.age.setValue(currentAgeValue + 1);
}

Working forked Plunker