[英]How I can set value before using RemoveAt(i) on Angular Reactive Form Array?
在 Angular Reactive Form Array 上使用 RemoveAt(i) 之前如何设置值?
removeCustomerPhone(custIndex: number, phoneIndex: number) {
// this.customerPhones(custIndex).at(phoneIndex).setValue({
// 'IsDeleted': true
// })
this.customerPhones(custIndex).removeAt(phoneIndex, {
emitEvent : false
});
}
以下是绑定手机的方法:-
customerPhones(custIndex: number): FormArray {
return this.Addresses()
.at(custIndex)
.get('Phones') as FormArray;
}
newPhone(): FormGroup {
this.phoneForm = this.formBuilder.group({
ID:0,
phone1: '',
addressId:0,
IsDeleted:false
});
this.phoneForm.valueChanges.subscribe(a=>{
a.stateEnum = State.Modified
});
return this.phoneForm;
}
````
要给 FormGroup 赋值,你可以使用setValue但你需要为 formGroup 的每个字段赋一个值:`
this.customerPhones(custIndex).at(phoneIndex)
.setValue({ID:..,phone1:...,addressId:...,IsDeleted:...})
如果你只想改变一个属性(o severals but not all)你使用patchValue
this.customerPhones(custIndex).at(phoneIndex)
.patchValue({IsDeleted:true})
您可以使用表单控件的 patchValue 方法来设置特定表单控件属性的值。 例如:
this.customerPhones(custIndex).at(phoneIndex).patchValue({
'IsDeleted': true
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.