4

Lets say, I want to add method to NgForm (class)

NgForm.prototype.markAsDirty = function (): void {
    let f: NgForm = this;
    Util.forEach(f.form.controls, (k, v: AbstractControl) => {
        v.markAsDirty(false);
    });
};

Is this somehow possible in typescript?

I am aware of:

but it works only for interfaces, not classes.

Community
  • 1
  • 1
Krzysztof Bogdan
  • 916
  • 7
  • 16

1 Answers1

7

In Angular and TypeScript you usually use inheritance like

export class MyForm extends NgForm {
  ...
}

and register your custom class to be used throughout your application instead of the original class.

bootstrap(AppComponent, [FORM_PROVIDERS, FORM_DIRECTIVES, provide(NgForm, { useClass: MyForm})]);

I haven't investigated if there any additional things to consider that are special to the NgForm, FORM_PROVIDERS or FORM_DIRECTIVES to make this work properly.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567