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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.