component.ts
ngAfterViewInit() { let phoneNumberInput = document.getElementById('sender-phone'); let inputmask = new Inputmask('+7(999)999-99-99'); inputmask.mask(element); }
Inputmask is working good. But my [(ngModel)] didn't work. Anyone know how I can resolve my issue ?
I created directive with Input Mask
`
@Directive({ selector: '[tdInputmask]' })
export class InputmaskDirective {
@Output('tdInputmaskUnmaskedValue') unmaskedValueEmitter = new EventEmitter();
This directive worked for me with Angular 5 dynamic forms. Haven't tried it with [(ngModel)] style template driven forms but I think it would work the same with those.
import {Directive, ElementRef, Input, OnDestroy, Optional} from '@angular/core';
import {NgControl} from '@angular/forms';
@Directive({
selector: '[inputMask]',
})
export class InputMaskDirective implements OnDestroy {
@Input('inputMask')
mask: string;
@Input('inputMaskOptions')
options: any;
constructor(
private elementRef: ElementRef,
@Optional() private control: NgControl,
) {}
ngOnChanges(): void {
if (!this.control) {
console.warn('No control for InputMaskDirective');
return;
}
if (this.mask) {
let options: any = _.clone(this.options) || {};
_.defaults(options, { showMaskOnHover: false });
options.oncomplete = options.onincomplete = options.oncleared = () => this.handleChange();
Inputmask(this.mask, options).mask(this.elementRef.nativeElement);
} else {
Inputmask.remove(this.elementRef.nativeElement);
}
}
private handleChange(): void {
this.control.control.patchValue(this.elementRef.nativeElement.value);
}
}
Hi @gregmagolan , I imported InputMaskDirective in my app-routing.module.ts file and declared in @NgModule({declarations: [InputMaskDirective] }). And my input box is <input type="text" inputMask="(999)-999-9999" />. Still it is not working. Where m I going wrong?
Hello everyone, I have some problem with ng2 ang inputmask. For example I have had this code component.html `
component.ts
ngAfterViewInit() { let phoneNumberInput = document.getElementById('sender-phone'); let inputmask = new Inputmask('+7(999)999-99-99'); inputmask.mask(element); }
Inputmask is working good. But my [(ngModel)] didn't work. Anyone know how I can resolve my issue ?
My code for example http://plnkr.co/edit/F3pob7hH2ZrJv0MDNa9x?p=preview
I have the same problem. Have you resolve it?
Yes, I have. http://plnkr.co/edit/tN6y5d2fsl0b7cRq6Prv?p=preview
I created directive with Input Mask ` @Directive({ selector: '[tdInputmask]' }) export class InputmaskDirective { @Output('tdInputmaskUnmaskedValue') unmaskedValueEmitter = new EventEmitter();
}`
And in HTML code I use
<input [tdInputmask]="'+7(999)999-99-99'" (tdInputmaskUnmaskedValue)="val3 = $event">
I use inputmask through primeng(http://www.primefaces.org/primeng/#/mask). I decided to use it later until it is fixed. thx~
A temporary solution:
how to get this working with autoUnmask: true option?
This directive worked for me with Angular 5 dynamic forms. Haven't tried it with
[(ngModel)]
style template driven forms but I think it would work the same with those.Usage (with dynamic forms) is like this:
May help someone.
And this is a similar directive for restrict input:
Usage like so:
where do you take Inputmask?
Its an @Input on the
inputMask
directive:<input formControlName="phoneNumber" inputMask="(999)-999-9999">
Hi @gregmagolan , I imported InputMaskDirective in my app-routing.module.ts file and declared in @NgModule({declarations: [InputMaskDirective] }). And my input box is
<input type="text" inputMask="(999)-999-9999" />
. Still it is not working. Where m I going wrong?