简体   繁体   English

Angular 9 动态禁用表单控件

[英]Angular 9 dynamically disable form control

What I'm trying to achive is after initiation of the form to disable some controls;我想要实现的是在启动表单后禁用某些控件;

this.someForm= this._fb.group({
  lastName: ['', Validators.required],
  firstName: ['', Validators.required]
});

<input type="text" formControlName="firstName" class="input-field"/>

this.someForm.controls.firstName.disable(); //not works
this.someForm.get('firstName').disable(); //not works

Is there a way to do that without recreating form?有没有办法在不重新创建表单的情况下做到这一点?

this.someForm.get('firstName').disable(); //not works

应该可以工作,您只需要在之后更新值和有效性:

this.someForm.get('firstName').updateValueAndValidity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM