Created
April 14, 2017 00:25
-
-
Save ChezRD/7c0ce3f0ee11fe37fd2cd0dc6f689686 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
/** | |
* @author Rodinei Fagundes <[email protected]> | |
*/ | |
import { Directive, HostListener, Input } from '@angular/core'; | |
import { NgControl } from '@angular/forms'; | |
@Directive({ | |
selector: '[custom-directive]' // Attribute selector | |
}) | |
export class CustomDirective { | |
@Input('textMask') | |
textMaskConfig = { | |
mask: [] | |
}; | |
constructor(private ngControl: NgControl) { } | |
@HostListener('blur', ['$event']) | |
onBlur($event: any) { | |
let value = this.ngControl.value; | |
// Workaround for https://github.com/text-mask/text-mask/issues/294 | |
if (value.length > this.textMaskConfig.mask.length) { | |
value = value.slice(0, -1); | |
} | |
this.ngControl.control.setValue(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment