如果您使用反应式表单,您可以动态分配它,就像
下面的示例代码(电子邮件字段)
this.registerForm = this.formBuilder.group({
first_name: ['', Validators.required],
last_name: ['', Validators.required],
email: new FormControl({value: null, disabled: true}, Validators.required),
password: ['', Validators.compose([Validators.required, Validators.email])],
confirm_password: ['', Validators.required],
});
如果您想获取包括禁用控件在内的所有值,您应该使用:
this.registerForm.getRawValue();
查看源码中的方法注释
/**
* The aggregate value of the `FormGroup`, including any disabled controls.
*
* If you'd like to include all values regardless of disabled status, use this method.
* Otherwise, the `value` property is the best way to get the value of the group.
*/
getRawValue(): any;
享受编码的乐趣!