0

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();
  }

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what this line means? "this.ingredients[id - 1].price". Why do you write "[id - 1]"? Thanks
because the row count from the template starts from 1 where as our array it starts with 0 this is the reason @Joseph

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.