Currently I am working in a project on Angular 2. I have to do like this.
In short, there are few fields data coming from database(row with field value 'test8'). after that if user want to add new fields and values then they have to click the "Add new row" button and save the data with the submit button.
The problem with me is that I cannot bind the dynamic data given by user to component and save that in database.
template file code is as follows:
<form class="form-inline" role="form" id="SeasonAddForm" #userlogin = "ngForm" (ngSubmit)="updateBrochurePriceHandler()">
<div class="secondListDiv">
<div class="row " *ngFor="let sl of secondResultList, let index = index;">
<div class="col-xs-4">
<input id="branch_text_second{{index}}" name="saveData2-{{index}}-branch_text" class="form-control rt no-radius" type="text" value="{{sl.branch_text}}" [(ngModel)]="sl.branch_text" placeholder="">
</div>
<div class="col-xs-4">
<input id="initial_service_second{{index}}" name="saveData2-{{index}}-initial_service" class="form-control rt no-radius" type="text" value="{{sl.initial_service}}" [(ngModel)]="sl.initial_service" placeholder="">
</div>
<div class="col-xs-4">
<input id="quarterly_charge_second{{index}}" name="saveData2-{{index}}-quarterly_charge" class="form-control rt no-radius" type="text" value="{{sl.quarterly_charge}}" [(ngModel)]="sl.quarterly_charge" placeholder="">
</div>
<input id="type{{index}}" name="saveData2-{{index}}-type" class="form-control rt no-radius" type="hidden" value="{{sl.type}}" [(ngModel)]="sl.type" placeholder="">
</div>
</div>
<div class="row">
<div class="col-xs-4">
<input type="button" value="Add New Row" (click)="addSecondListRow()" class="frm_sub" id='addNewSecondDiv' >
</div>
</div>
</form>
Component file code is here
public firstResultList:any;
public secondResultList:any;
private newFirstAttribute: any = {};
private newSecondAttribute: any = {};
addSecondListRow(){
this.newSecondAttribute.branch_text='';
this.newSecondAttribute.initial_service='';
this.newSecondAttribute.quarterly_charge='';
this.newSecondAttribute.type='2';
this.secondResultList.push(this.newSecondAttribute);
}
updateBrochurePriceHandler(formData) {
this.http.post(CONSTANTS.baseApiUrl +
'Angular2_api/update_brochure_price', formData)
.map(res => res.json())
.subscribe((data) => {
console.log(data);
})
}