如果想要替换掉已经存在的
control
, 应该使用setControl()
。
const group = new FormGroup({
groupName: new FormControl('CCB'),
});
const control = group.get('groupName');
-
addControl(name: string, control: AbstractControl): void
// groupName 这个 control 的名字已经存在了, 再去 addControl() 是不会被新的替换掉的 group.addControl('groupName', new FormControl('C1C')); // still CCB
-
setControl(name: string, control: AbstractControl): void
// 如果想要替换掉已经存在的 control, 应该使用 setControl() group.setControl('groupName', new FormControl('C1C')); // will change into C1C