Skip to content

Instantly share code, notes, and snippets.

@Yurii-Chaban
Last active September 18, 2018 11:50
Show Gist options
  • Save Yurii-Chaban/59055cc07270547af0a3a5b34e982124 to your computer and use it in GitHub Desktop.
Save Yurii-Chaban/59055cc07270547af0a3a5b34e982124 to your computer and use it in GitHub Desktop.
import { Directive, Attribute, HostListener } from '@angular/core';
import { NgModel } from '@angular/forms';
@Directive({
selector: '[mask]',
host: {
'(keyup)' : 'onInputChange()'
}
})
export class MaskDirective{
modelValue: string;
constructor(public model:NgModel, @Attribute('mask')modelValue: string){}
onInputChange(){
this.modelValue = this.getModelValue();
this.model.viewToModelUpdate(this.modelValue);
this.model.valueAccessor.writeValue(this.modelValue);
}
getModelValue(){
let modelValue = this.model.value;
return modelValue.replace(/\s{2,}/g, ' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment