直到“最终”2.0的角度我已经做到了:
<input type="text" formControlName="name" [disabled]="!showName">
动态禁用/启用表单输入.
从Rc7升级到2.0后,我在控制台窗口中得到这个警告:
It looks like you’re using the disabled attribute with a reactive form directive. If you set disabled to true
when you set up this control in your component class,the disabled attribute will actually be set in the DOM for
you. We recommend using this approach to avoid ‘changed after checked’ errors.
我已经改变了我的代码,按照这些说明:
this._userGroupUsersForm = this._formBuilder.group({ 'users': [{'',disabled: this.showName},Validators.compose([Validators.required,Validators.minLength(3),Validators.maxLength(50),Validators.pattern("^[a-zA-ZåäöÅÄÖ 0-9_-]+$")])] });
这对于初始页面加载工作正常,但是我不能再像这样切换状态:
toggleName() : void { this.showName = !this.showName; }
我该如何解决?
注意:我的“旧”方式(通过设置[disabled])不起作用.
这应该工作
原文链接:https://www.f2er.com/angularjs/140464.htmltoggleName() : void { let ctrl = this.form.get('name') ctrl.enabled ? ctrl.disable() : ctrl.enable() }