0
我正在嘗試在angular 4中使用簡單的數組形式,並且我遇到了ngModel的一個奇怪問題。Angular 4,在表單數組中使用ngModel(ReactiveFormsModule)
請首先鎖定我的密碼。
我的方法
定義我的表單FormGroup:
public invoiceForm: FormGroup;
設置我的測試數據和表單型號:
readonly local = [ 'Lorem ipsum dolor sit amet', 'et iusto odio dignissim qui blandit', 'Epsum factorial non deposit', 'Ma quande lingues coalesce' ]; public localModel = this.local;
定義表單組在ngOnInit和簡單的添加我的本地人排入表格組:
this.invoiceForm = this._fb.group({ itemRows: this._fb.array([this.initItemRows()]) }); for (let index in this.local) this.setAndReplaceNewERow(+index)
現在,一切看起來都正確。 see result as image
的問題
當我改變的文本輸入中的一個,主要變量也將改變。
變更前:
FormGroup
{
"itemRows": [
{
"itemname": "Lorem ipsum dolor sit amet"
},
{
"itemname": "et iusto odio dignissim qui blandit"
},
{
"itemname": "Epsum factorial non deposit"
},
{
"itemname": "Ma quande lingues coalesce"
}
]
}
My Local Data
[
"Lorem ipsum dolor sit amet",
"et iusto odio dignissim qui blandit",
"Epsum factorial non deposit",
"Ma quande lingues coalesce"
]
My Form Model
[
"Lorem ipsum dolor sit amet",
"et iusto odio dignissim qui blandit",
"Epsum factorial non deposit",
"Ma quande lingues coalesce"
]
變更後:
FormGroup
{
"itemRows": [
{
"itemname": "Lorem ipsum dolor sit amet"
},
{
"itemname": "et iusto odio dignissim qui blandit"
},
{
"itemname": "changed !!!!!!!!!!!"
},
{
"itemname": "Ma quande lingues coalesce"
}
]
}
My Local Data
[
"Lorem ipsum dolor sit amet",
"et iusto odio dignissim qui blandit",
"changed !!!!!!!!!!!",
"Ma quande lingues coalesce"
]
My Form Model
[
"Lorem ipsum dolor sit amet",
"et iusto odio dignissim qui blandit",
"changed !!!!!!!!!!!",
"Ma quande lingues coalesce"
]
DEMO
請看看Plunk Demo
謝謝Alex。那正是我想要的。 – raminmohammadi