import { Directive } from '@angular/core';
import { NgModel } from '@angular/forms';
@Directive({
selector: '[ngModel][my-directive]',
providers: [NgModel],
host: {
'(ngModelChange)': 'onInputChange($event)'
}
})
export class MyDirective {
constructor(private model: NgModel) { }
onInputChange(value) {
console.log(value);
this.model.valueAccessor.writeValue(value.replace(/[^\d\.\,\s]+/g, ''));
}
}
Then to use it on HTML part:
<input name="productPrice" [(ngModel)]="price" my-directive />

No comments:
Post a Comment