gpt4 book ai didi

angular - 如何使 formControl 只读

转载 作者:太空狗 更新时间:2023-10-29 16:57:01 25 4
gpt4 key购买 nike

如何在 Angular 只读中制作 formControl

我知道我可以用 html 来做

<input type="text" formControlName="xyz" readonly />

如何从 JS 代码而不是 html,即以模型驱动的方式做到这一点

最佳答案

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],
});

如果你想获得所有的值,包括禁用的控件,你应该使用:

    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;

享受编码吧!

关于angular - 如何使 formControl 只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45452175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com