我有关于从输入类型获取值的问题=" text"在ngFor中,当我用[(ngModel)]绑定它时,所有它都具有相同的值,我如何绑定ngFor中的所有输入?
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>
成分</ P>
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
答案 0 :(得分:1)
如果我理解这一点,您希望将输入绑定到listServiceFeature
数组的成员。是对的吗?如果这是你想要做的,你可以使用索引直接绑定到数组成员:
<input class="form-control" [(ngModel)]="listServiceFeature[i]" type="text" />
现在,如果您向添加的输入添加一些文本并点击保存,您将在控制台上获得整个数组。
答案 1 :(得分:0)
经过长时间的搜索,如果您使用的是更新版本的angular(Angular 4),则必须导入FormModule
Angular 4 - "Can't bind to 'ngModel' since it isn't a known property of 'input' " error