我想一個字符串數組從我的輸入綁定,所以在HTML文件中我寫了這一點:Angular2 ngModel內的ngFor
<div *ngFor="let word of words; let in=index" class="col-sm-3">
<div class="form-group">
<input type="text" [(ngModel)]="words[in]" class="form-control" [attr.placeholder]="items[in]" required>
</div>
</div>
但因爲當我登錄的預期並沒有工作words變量它顯示一個空數組,在我的Component類中初始化。另外,我應該從另一個組件記錄變量,應該是我的問題。我有兩個組件:
- 包含查詢組件數組的表單組件。
- 具有單詞字符串數組的查詢子組件。
因此,詞語變量聲明到查詢部件,但我記錄通過窗體部件這樣這個變量:
console.log(JSON.stringify(this.queries));
雖然查詢是查詢的形式分量的數組:
queries:Query[] = [];
感謝您的幫助!
對我來說,問題不明確。如果你能明確告訴你究竟想要什麼? – micronyks
經過多次測試後,我的問題是在更新後將查詢字發送回我的表單組件 –
每個輸入標籤必須具有定義的唯一名稱屬性。 http://stackoverflow.com/questions/39336708/angular2-ngmodel-inside-ngfor-data-not-binding-on-input –