2016-09-24 49 views
1

我在使用ngForFor從input type =「text」獲取值時存在問題,而我將它與[(ngModel)]綁定時,它們都具有相同的值我可以在ngFor中綁定所有輸入嗎?如何從input type =「text」在循環ng中獲取輸入值對於

HTML

<button (click)="saveFeature()" type="button">save</button> 

<input class="form-control" type="text" /> 
<button (click)="addFeature()" type="button">Add</button> 

<div *ngFor="let inputservice of servicesfeature_id; let i=index"> 
    <input class="form-control" [(ngModel)]="listServiceFeature" type="text" /> 
    <button (click)="RemoveFeature(i)" type="button">Remove</button> 
</div> 

組件

servicesfeature_id: any = []; 
servicesfeature_length: number = 0; 
listServiceFeature: any = []; 
servicefeature_name: string; 

saveFeature(): void { 
    console.log(this.listServiceFeature); 
} 

addFeature(): void { 
    this.servicesfeature_id.push('service' + this.servicesfeature_length); 
    this.servicesfeature_length += 1; 
} 

RemoveFeature(index): void { 
    this.servicesfeature_length -= 1; 
    this.servicesfeature_id.splice(index, 1); 
} 

這裏是代碼plnkr.co

回答

2

如果我理解這一點,你想擁有的輸入綁定到listServiceFeature陣列的成員。是對的嗎?如果你添加一些文字,以增加輸入和點擊保存你的控制檯對整個陣列

<input class="form-control" [(ngModel)]="listServiceFeature[i]" type="text" /> 

現在:如果這是你想要做什麼,你可以直接綁定到使用索引陣列成員。

相關問題