Last active
September 18, 2018 11:50
-
-
Save Yurii-Chaban/59055cc07270547af0a3a5b34e982124 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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