I need help with Angular directive reload. I have a Angular Directive that not work when my ´format´ attribute was inserted after Directive load: (that Directive work normaly when I insert the attributte directly on html)
// imports here
// example use: <input type="text" id="num" format="***,***,***,**" formControlName="num" class="form-control form-control-sm">
@Directive({
selector: '[format]'
})
export class FormatNumberDirective implements OnChanges{
subscription: Subscription;
@Input('format') valueFormat:any;
constructor(
private controlDir: NgControl,
private renderer: Renderer2,
private host: ElementRef
){}
ngOnInit() {
this.listenEvents();
}
listenEvents(){
if (this.host != null) {
this.renderer.listen(this.host.nativeElement, 'blur', (event) => {
let nbr = this.SFFmtNumber(this.control.value, this.valueFormat);
this.control.setValue(nbr.toString().trim());
});
}
}
get control() {
return this.controlDir.control;
}
SFFmtNumber(number, mask)
{
// some code to receive number and mask and return formated number.
}
}
I need to reload Angular format Directive because I insert the ´format´ attribute through mi ts component file. this way:
let numNative = this.el.nativeElement.querySelector('input#num');
this.renderer.setAttribute(numcliNative, 'format', "***,***,***,**9.99");
Important: I don't want insert the attribute directly on html.