我的表单组代码如下
this.myForm = this._fb.group({
branch_name: ['', [Validators.required]],
branch_timing: this._fb.array([
this.initBranchTiming(),
])
});
initBranchTiming() {
return this._fb.group({
day: ['', []],
open_from: ['00:00:00', []],
open_till: ['00:00:00', []]
});
}
这段代码更新了branch_name
(<FormControl>this.myForm.controls['branch_name']).updateValue(this.branch_detail.branch_name);
现在我必须更新表单数组的“日”字段。如何更新表单数组branch_timing的“day”字段?
AFAIK把一个FormGroup
里面的一个FormArray
去掉它们名称中的“FormControls”,使其只是一个列表,就像普通数组一样。
为了更新FormControls
单独更新每个的值AbstractControl
来自FormGroup
通过使用索引:
let index = 0; // or 1 or 2
(<FormArray>this.myForm.controls['branch_timing']).at(index).patchValue('example');
或者您可以更新整个FormArray
通过致电setValue
or patchValue
:
(<FormArray>this.myForm.controls['branch_timing']).setValue(['example', 'example1', 'example2']);
setValue
需要一个与整个结构或FormArray
, while patchValue
可以采用数组的超集或子集。 (Angular2 网站上的 FormArray 类 https://angular.io/docs/ts/latest/api/forms/index/FormArray-class.html)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)