Skip to content

Instantly share code, notes, and snippets.

@ChezRD
Created April 14, 2017 00:25
Show Gist options
  • Save ChezRD/7c0ce3f0ee11fe37fd2cd0dc6f689686 to your computer and use it in GitHub Desktop.
Save ChezRD/7c0ce3f0ee11fe37fd2cd0dc6f689686 to your computer and use it in GitHub Desktop.
/**
* @author Rodinei Fagundes <rodinei.jf@gmail.com>
*/
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