cesarrew / ng2-currency-mask

A very simple currency mask directive that allows using a number attribute with the ngModel.
MIT License
235 stars 116 forks source link

setValue being called before inputHandler gets initialized #159

Open williamxsp opened 3 years ago

williamxsp commented 3 years ago

I have a custom input that im trying to implement this directive. It works but I got this error message at the console.

ERROR TypeError: Cannot read property 'setValue' of undefined at CurrencyMaskDirective.writeValue (currency-mask.directive.ts:144) at CurrencyInputComponent.writeValue (control-value-accessor-connector.ts:26)

The problem is here:

writeValue(value: number): void {
        this.inputHandler.setValue(value);
    }

looks like this function is beeing fired before inputHandler gets initialized:

ngOnInit() {
        this.inputHandler = new InputHandler(this.elementRef.nativeElement, (<any>Object).assign({}, this.optionsTemplate, this.options));
    }

Ive created an stackblitz with the problem: https://stackblitz.com/edit/angular-ivy-ycax21?file=src%2Fapp%2Fapp.component.ts

Do you have any idea whats going on?