【问题标题】:Angular 9 dynamically disable form controlAngular 9 动态禁用表单控件
【发布时间】:2020-10-16 05:02:18
【问题描述】:

我想要实现的是在表单启动后禁用某些控件;

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

有没有办法在不重新创建表单的情况下做到这一点?

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:
this.someForm.get('firstName').disable(); //not works

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

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

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 2019-01-26
    • 2018-02-21
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多