Search code examples
angularangular-reactive-formsangular4-forms

patching value inside of rows in angular 4


How can i patch the value inside of my row. I have this unit_price that if i selected a specific ingredient in the select option, it would patch it in the input field of the unit_price? Pls see this link for the codes SEE THIS LINK

onSelectIngredient(event): void {
    const formData ={
      ingredient_id: event.target.value
    }
    console.log(formData);
    this.patchValues();
  }

Solution

  • Make the following Changes to the patchValue method.

     patchValues(id,i) {
        let x = (<FormArray>this.addForm.controls['rows']).at(i);
        console.log(x);
    
        x.patchValue({
          unit_price: this.ingredients[id - 1].price
        });
      }
    
    
    
      onSelectIngredient(event,i): void {
        const formData = {
          ingredient_id: event.target.value
        }
        console.log(formData,i);
        this.patchValues(event.target.value,i);
      }
    

    Template Changes

    <select (change)="onSelectIngredient($event,i)" class="form-control" formControlName="ingredient_id"> // to get the row id
    

    Working example in link