我已經成功設置了一個Formarray,它有兩個控件。這些控件在HTML表單中設置爲選擇對象。我可以成功推送和顯示多個元素集,但是當我嘗試更改Formarray的第一個元素中的select對象的值時,其他Formarray元素選擇對象的值相同。我正在使用[(ngModel)]將值綁定到對象,我相信這就是爲什麼值總是相同的原因。我嘗試使用[(ngModel)] = stringArray [i]使用Formarray索引的數組變量,但是當頁面加載時出現錯誤。在Angular版本4中如何使用ngModel與Formarray選擇對象
任何人都可以建議如何使用[(ngModel)]或其他機制來獲取選擇對象的值嗎?我正在使用ng-lightning選擇組件(版本1.3.0)和SLDS Lightning Design CSS。我正在使用Angular版本4.1.3。在我的應用程序的其他部分,我需要使用[(ngModel)]和一個字符串來獲取選擇對象的值。選擇值是一個對象數組,而不是像字符串這樣的基本變量。該組件沒有定義onSelect()函數。
HTML:
<form [formGroup]="callFlowForm">
<div formArrayName="callDevices">
<!-- Check the correct way to iterate your form array -->
<div *ngFor="let callDevice of callFlowForm.controls.callDevices.controls; let i=index" [formGroupName]="i">
<div class="form-group">
<div class="slds-form-element slds-size--8-of-8">
<ngl-form-element label="Device #: {{ i + 1 }}" class="slds-m-top--small">
<select nglFormControl class="slds-select"
formControlName="callAsset"
[(ngModel)]="selectedDevice"
ngDefaultControl >
<option *ngFor="let device of devices"
[value]="device.id">{{device.assetName}}
</option>
</select>
</ngl-form-element>
</div>
</div>
<div class="form-group">
<div class="slds-form-element slds-size--8-of-8">
<ngl-form-element label="Protocol:" class="slds-m-top--small">
<select nglFormControl class="slds-select"
formControlName="assetTransport"
[(ngModel)]="selectedTransport"
ngDefaultControl >
<option *ngFor="let protocol of transports"
[value]="protocol.id">{{protocol.type}}
</option>
</select>
</ngl-form-element>
</div>
</div>
<button *ngIf="callFlowForm.controls.callDevices.controls.length > 1" (click)="deleteDevice(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button type="button" (click)="addDevice()" class="btn btn-primary">Add Device</button>
</form>
組件:
selectedTransport: string;
selectedDevice: string;
public callFlowForm: FormGroup;
transports: SipTransport[] = [{'id': 1, 'type': 'UDP'},
{'id': 2, 'type': 'TCP'},
{'id': 3, 'type': 'TLS'}
];
devices: Asset[] = [{'id': 1, 'assetName': 'VCS', 'type': 'SIP Registrar'},
{'id': 1, 'assetName': 'CUCM', 'type': 'Call Control'},
{'id': 1, 'assetName': 'SBC', 'type': 'Call Control'},
{'id': 1, 'assetName': 'EX60', 'type': 'Endpoint'}
];
constructor(private dialService: DialService,
private formBuilder: FormBuilder
) { }
ngOnInit() {
this.selectedDevice = '1';
this.selectedTransport = '1';
this.callFlowForm = this.formBuilder.group({
callDevices: this.formBuilder.array([this.addDevices()]) // here
});
}
addDevices() {
return this.formBuilder.group({
callAsset: [''],
assetTransport: ['']
});
}
addDevice() {
const control = <FormArray>this.callFlowForm.controls['callDevices'];
control.push(this.addDevices());
}
deleteDevice(index: number) {
const control = <FormArray>this.callFlowForm.controls['callDevices'];
control.removeAt(index);
}
你是絕對正確的,這是因爲ngModel的。雙向結合在反應形式中極不鼓勵。我無法理解爲什麼你需要雙向綁定。爲什麼不使用formcontrol來代替。這就是應該使用:) – Alex
嗨@ AJT_82,我不需要ngModel,但我已經在其他形式使用它,特別是在編輯形式,我想預先填充字段與當前值編輯。您的建議可以正常工作,因爲我可以在不使用ngModel的情況下對所選的值進行console.log。我只需要弄清楚如何在提交時迭代組件中的formarray數據。我在控制檯中得到了這個:這是我的表單{「callDevices」:[{「callAsset」:「24」,「assetTransport」:「2」},{「callAsset」:「34」,「assetTransport」:「1 「}]} –
您也可以使用表單控件設置預選值,或者在設置值之前不建立表單。或者,當你收到它們時,通過修補值的形式,如果這是異步的。但關於你的問題...... *我只需要弄清楚如何在提交時迭代組件中的formarray數據*爲什麼你需要迭代它?我的意思是你想在迭代中完成什麼?作爲一個側面說明它就像迭代任何數組:) – Alex