![](/img/trans.png)
[英]how to disable a form element of formArray in reactive formGroup using Angular5
[英]Angular reactive form - Disable material design buttons in FormGroup
我有一个包含Angular材质设计组件的反应形式。 我想在初始化组件1时禁用窗体中的所有控件。
我试图在构造函数以及ngOnInit()中的FormGroup上调用disable(),但是由于某些原因,表单中的按钮(使用材质设计)保持启用状态。
模板:
<form [formGroup]="emailForm">
<mat-form-field>
<input matInput type="email" formControlName="email" placeholder="Email" />
</mat-form-field>
<button formControlName="removeButton" mat-icon-button class="button-small" (click)="clearEmail()" matTooltip="Clear email address" matTooltipClass="tooltip-medium" [matTooltipShowDelay]="500" ngDefaultControl>
<mat-icon aria-label="Clear">clear</mat-icon>
</button>
</form>
组件:
export class EmailFormComponent implements OnInit {
private emailForm: FormGroup;
constructor(private formbBuilder: FormBuilder) {
this.createReactiveForm();
}
ngOnInit() {
this.emailForm.disable();
}
createReactiveForm() {
this.emailForm = this.formbBuilder.group({
email: ['', [Validators.email, Validators.maxLength(100)]],
removeButton : ['']
});
this.emailForm.disable();
}
clearEmail() {
this.emailForm.patchValue({email:''});
}
}
如果我从按钮上删除了材料设计,它将在初始化期间被禁用。 如果我从组件中的单击eventHandler方法调用this.emailform.disable()
,则表单中的按钮也会被禁用。
是在初始化期间未禁用按钮的错误还是我做错了什么? 初始化时如何在用户界面中禁用按钮?
(注意:这是一个简化的示例。实际的应用程序在表单中具有更多输入字段)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.