2017-02-02 42 views
0

我有一個表格,它使用*ngFor來遍歷數組以創建每個項目的輸入字段。ngModelChange不會更新輸入的值

該數組以1個項目開頭,但是當輸入一個數字時,根據此數字將更多項目添加到數組中。

我使用(ngModelChange)在輸入號碼時將新項目推送到數組。

<form #testForm="ngForm" (submit)="submit()"> 
    <div *ngFor="let test of tests"> 
     <label for="testProp">Test Property</label> 
     <input type="number" [(ngModel)]="test.testProp" (ngModelChange)="inputChange($event)" name="testProp"/> 
     <b>Value: {{test.testProp}}</b> 
    </div> 
    </form> 

inputChange功能

inputChange(number: any, index: any) { 

    for(var i = 0; i < number; i++) { 
    console.log(i); 
    this.tests.push({}); 
    } 

} 

當前行爲

當結合該項目的價值是存在的,但由於某些原因,輸入的值是空白。

預期的行爲

輸入的值不爲空。

問題的最小再現與指令

http://plnkr.co/edit/5P0nt68FSFH2UuEpJRnh?p=preview

  • 角版本: 2.4.5

  • 瀏覽器:所有

  • 語言:打字稿2.0.2

+0

我在angular repo創建了一個問題:https://github.com/angular/angular/issues/14251 –

+0

嘗試刪除括號[ngModel] =「test.testProp」 –

回答

1

我用對每個輸入相同name屬性。將名稱添加到名稱上name="testProp-{{i}}"