Angular 如何对form.group及其控件进行故障排除
我有一个可重用的控件,它基于mat select控件,我使用该控件将一个可观察对象作为数据源传递,因此我有一种快速的方法将其放到我的任何表单上。除了控件的禁用部分外,其他都可以正常工作。我遇到了一些问题。如果我将控件设置为禁用,则不会影响它。下面是一些相关的代码,如果需要更多,我会更新问题 在父窗体上,我这样设置控件 恩戈尼特{Angular 如何对form.group及其控件进行故障排除,angular,angular-material,Angular,Angular Material,我有一个可重用的控件,它基于mat select控件,我使用该控件将一个可观察对象作为数据源传递,因此我有一种快速的方法将其放到我的任何表单上。除了控件的禁用部分外,其他都可以正常工作。我遇到了一些问题。如果我将控件设置为禁用,则不会影响它。下面是一些相关的代码,如果需要更多,我会更新问题 在父窗体上,我这样设置控件 恩戈尼特{ this.form = this.fb.group({ sampleguid: [{value: '', disabled: true} , Val
this.form = this.fb.group({
sampleguid: [{value: '', disabled: true} , Validators.required]
})
因此,我的问题是,如何检查父窗体中的禁用值是否进入自定义控件?
这是我的模板文件中的代码
<app-mat-dropdown
placeholder="Email Type"
appearance="fill"
[clear]="true"
label="Phone Type Label"
[multiple]="true"
[disabled]="disabled"
[options]="emailTypeOptions$ | async"
formControlName="sampleguid"
>
</app-mat-dropdown>
我的MatDropDownComponent扩展了我的BaseControlValueAccessor
import {ControlValueAccessor} from '@angular/forms';
export class BaseControlValueAccessor<T> implements ControlValueAccessor {
public _disabled = false;
set disabled(isDisabled: boolean) {
this._disabled = isDisabled;
}
/**
* Call when value has changed programmatically
*/
public value: T;
public onChange(newVal: T) {
}
public onTouched(_?: any) {
}
/**
* Model -> View changes
*/
public writeValue(obj: T): void {
this.value = obj;
}
public registerOnChange(fn: any): void {
this.onChange = fn;
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
public setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
}
是否有一个控件的formcontrolname设置为表单生成器定义的sampleguid?如果是,请查看。如果不是,这可能就是它不工作的原因。是的,我有一些测试控件,我只是复制了一个片段,我在示例中修复了它