【问题标题】:Angular 4 Inherited FormcontrolAngular 4 继承的表单控件
【发布时间】:2017-07-01 03:58:58
【问题描述】:

我正在尝试创建一个带有一些附加属性的 FormControl 子类,然后可以在我的自定义表单控件中使用这些属性来更改行为。

我尝试如下从 FormControl(比如 StandardFormControl)继承并用于创建表单组,但是当我在指令/其他任何地方访问 formcontrol 时,我没有获得子类表单控件的属性。

class StandardFormControl extends FormControl{
   customProperty: string
}

表单组创建如下

new FormGroup({
  firstName: new StandardFormControl('',[])
});

有人有什么想法吗?

【问题讨论】:

  • 它有什么关系,我说的是反应形式。
  • 跟踪控制状态是让表单反应的方式

标签: angular angular4-forms


【解决方案1】:

据我所知,您必须对表单控件进行类型转换:

// first create the form group and store it in a variable:
const formGroup = new FormGroup({
  firstName: new StandardFormControl('',[])
});

// then you can access its controls:
(formGroup.get('firstName') as StandardFormControl).customProperty = 'customValue';

【讨论】:

    【解决方案2】:

    你必须在构造函数中调用 super() 来继承父类。

    class StandardFormControl extends FormControl{
    
        customProperty: string
    
        constructor() {
           super();
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 2010-11-25
      • 2013-06-16
      • 2018-10-15
      • 2018-03-01
      • 2010-12-17
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多