Steve
Steve

Reputation: 31

Angular 4 Inherited Formcontrol

I am trying to create a subclass of FormControl with some additional properties which can then be used inside my custom form controls to change the behaviour.

I tried to inherit from FormControl(say StandardFormControl) as below and used to create a form group but when I access the formcontrol inside a directive/anywhere else, I do not get the properties of the subclassed form control.

class StandardFormControl extends FormControl{
   customProperty: string
}

The form group was created as below

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

Anyone has any ideas?

Upvotes: 3

Views: 2978

Answers (2)

sl4very
sl4very

Reputation: 11

You have to call super() in constructor to inherit the parent class.

class StandardFormControl extends FormControl{

    customProperty: string

    constructor() {
       super();
    }    
}

Upvotes: 1

Marco
Marco

Reputation: 99

As far as I know you have to typecast the form control:

// 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';

Upvotes: 5

Related Questions