如何使FormControl变为只读 [英] How to make a formControl readonly
本文介绍了如何使FormControl变为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使angular的formControl只读
How to make a formControl in angular readonly
我知道我可以像
<input type="text" formControlName="xyz" readonly />
如何通过JS代码而不是html(例如,以模型驱动方式)
how to do it from JS Code and not html i.e in a model driven way
推荐答案
如果您使用的是反应式表格,则可以像在 下面的示例代码(电子邮件字段)
If you are using Reactive Forms you can assign it dynamically like in the example code below (email field)
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],
});
如果要获取所有值,包括禁用的控件,则应使用:
If you want to get all the values including disabled controls you should use:
this.registerForm.getRawValue();
查看源代码上的方法注释
View method comment on source code
/**
* 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;
享受编码!
这篇关于如何使FormControl变为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文