角4继承Formcontrol

问题描述 投票:2回答:2

我想创建FormControl用,然后可以在我的自定义窗体控件可以用来改变行为的一些附加属性的子类。

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

class StandardFormControl extends FormControl{
   customProperty: string
}

形式组已创建如下面

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

任何人有任何想法?

angular angular4-forms
2个回答
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';

0
投票

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

class StandardFormControl extends FormControl{

    customProperty: string

    constructor() {
       super();
    }    
}
© www.soinside.com 2019 - 2024. All rights reserved.