I have this markup:
<div *ngFor="let quantity of model.quantities">
<div class="form-group kt-form__group row">
<label class="col-lg-2 col-form-label">Quantity:</label>
<div class="col-lg-6">
<mat-form-field class="example-full-width">
<input matInput [(ngModel)]="quantity.name" name="quantityname" placeholder="Enter Quantity" type="text">
</mat-form-field>
</div>
</div>
<div class="form-group kt-form__group row">
<label class="col-lg-2 col-form-label">Price:</label>
<div class="col-lg-6">
<mat-form-field class="example-full-width">
<input matInput [(ngModel)]="quantity.price" name="quantityprice" placeholder="Enter Price" type="text">
</mat-form-field>
</div>
</div>
<hr />
</div>
Every time I'm adding a new object of type quantity in the model, for each object in the array the same value is set, the one which is entered as last https://screencast-o-matic.com/watch/cqQ1Fbt4zL
This is the button click event:
addNewQuantity() {
if (this.newQuantity) {
let quantity = new PriceGridQuantity();
quantity.name = this.newQuantity;
this.model.quantities.push(quantity);
this.newQuantity = '';
}
}
Update: model added:
model: any = {
TenantId: '',
CustomFieldName: '',
quantities: []
};
You should be using FormBuilder and FormControls in Angular, but here's something that might Work
COMPONENT TS
HTML