fra*_*nco 14 angular2-forms angular
我有一个选择控件,我想根据条件动态禁用:
this.activityForm = this.formBuilder.group({
docType: [{ value: '2', disabled: this.activeCategory != 'document' }, Validators.required]
});
Run Code Online (Sandbox Code Playgroud)
但是,即使在某些时候this.activeCategory变为"document",docType也不会启用.
我该如何解决?
dev*_*033 33
既然我不知道你是怎么操纵的activeCategory(也许它也是一个FormControl?),我会建议采用以下方法:
您可以使用它(change)来检测何时this.activeCategory发生了变化,如下所示:
1 - 如果您正在使用ngModel:
<input type="text" [(ngModel)]="activeCategory" (change)="checkValue($event)">
Run Code Online (Sandbox Code Playgroud)
2 - 如果是FormControl:
<input type="text" formControlName="activeCategory" (change)="checkValue($event)">
Run Code Online (Sandbox Code Playgroud)
因此,在组件中,您可以使用以下方法操作docType 控件disable/enable:
checkValue(event: Event) {
const ctrl = this.activityForm.get('docType');
if (event.value === 'document') {
ctrl.enable();
} else {
ctrl.disable();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39432 次 |
| 最近记录: |