I am trying to add rows to a form. Each row has 3 input fields.
This is what I've done so far.
export class MedicationGrid {
title1:string;
title2:string;
title3:string;
}
component html
<li class="am-row" *ngFor="let dynamic of dynamicArray; let i = index;">
<div class="medication-name">
<input type="text" class="form-control am-medicine" placeholder="Medicine" [(ngModel)]="dynamicArray[i].title1" name="medicine" required />
</div>
<div class="medication-dosage">
<input type="text" class="form-control am-dosage" placeholder="Dosage" [(ngModel)]="dynamicArray[i].title2" name="dosage" />
</div>
<div class="medication-frequency">
<input type="text" class="form-control am-frequency" placeholder="Frequency" [(ngModel)]="dynamicArray[i].title3" name="frequency" />
</div>
<div class="medication-add">
<img src="../../../assets/img/actionModal/add.png" (click)="addMedicationRow(i)" />
</div>
</li>
component.ts
ngOnInit() {
this.newDynamic = {title1:"", title2:"", title3:""};
this.dynamicArray.push(this.newDynamic);
}
addMedicationRow(index) {
this.newDynamic = {title1:"", title2:"", title3:""};
this.dynamicArray.push(this.newDynamic);
return true;
}
I can add new rows with this. But the problem is, when i enter some data in to the fields and click the add button, all the data is gone along with the new row is inserted. How do I fix this?