【发布时间】:2020-06-30 17:19:49
【问题描述】:
我有这个自定义组件:
<my-component [control]="..."></my-component>
这里,控制定义为:
@Input() control: FormControl;
my-component 的使用:
this.myFormGroup = new FormGroup({
name: new FormControl('')
});
<my-component [control]="myFormGroup.controls.name"></my-component>
错误:
Angular 10 严格模式抱怨“myFormGroup.controls.name”不是 FormControl。
“控件”在 FormGroup 中定义为一个对象,其中每个字段都是 AbstractControl 类型:
// forms.d.ts
export declare class FormGroup extends AbstractControl {
controls: {
[key: string]: AbstractControl;
};
// ....
}
这段代码可以在运行时工作,但不能编译。
解决这个问题的最佳方法是什么?
【问题讨论】:
-
我知道严格模式应该是更好的做事方式,但解决非常简单的操作,如答案所暗示的对我来说似乎真的很hacky。有谁知道 Angular 官方是如何推荐这样做的?
标签: angular angular-reactive-forms