I want all objects of the FormControl
type to have the observable (or event emitter) field (e.g. onMarkAsDirty
) which will be triggered on markAsDirty
method call. I tried to implement this by extending FormControl
prototype, but it is too hard for me. Can you help me please and write some sketches?
CodePudding user response:
Extends a class is easy. You has the code of the FormControl (Really of AbstractControl) in github
So you can define
export class FormControlExtends extends FormControl {
markAsDirty(opts: { onlySelf?: boolean } = {}): void {
(this as { pristine: boolean }).pristine = false;
if (this['_parent'] && !opts.onlySelf) {
this['_parent'].markAsDirty(opts);
}
//make some more
console.log('say hello');
}
}
See that to access to a private variable (in this case "_parent") use square bracket, e.g. not this._parent else this['_parent']
And you can use as
control=new FormControlExtends(null)