I created the following form in order to add items to an array of ingredients. When the user clicks " Add a new Ingredient ", an empty ingredient is added to the list.
HTML:
<button md-button
(click)="addIngredient()">
<md-icon>add</md-icon> ADD
</button>
<div *ngFor="let ingredient of ingredients">
<md-form-field floatPlaceholder="never">
<input mdInput placeholder="NAME"
[(ngModel)]="ingredient.name" name="name">
</md-form-field>
<md-form-field floatPlaceholder="never" style="width: 80px">
<input mdInput placeholder="QUANTITY" type="number"
[(ngModel)]="ingredient.quantity" name="quantity">
</md-form-field>
</div>
TS:
addIngredient() {
// outputs real list
console.log(this.ingredients);
this.ingredients.push({
name: 'tt',
quantity: '12'
});
}
The app starts, and I click "Add" to add a new empty ingredient to the list. I updated the ingredient name and quantity, and then click "ADD" again to add another ingredient.
when I click the "ADD" button to add another ingredient, the form inputs of all the previous ingredients I added are set to the values I declared in the addIngredient()
method (all names are showing as 'tt' and quantity is '12') although console log shows the real list.
Any idea what causes this issue?