Installation and usage
Angular 2 Input Mask
Getting started
First, install it.
npm i angular2-text-mask --save
Then, import it into your @NgModule
:
import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { TextMaskModule } from 'angular2-text-mask'; @NgModule({ imports: [ FormsModule, TextMaskModule ], declarations: [] }) export class MyModule {}
Then, use it in your component:
@Component({ selector: 'app', template: ` <input [textMask]="{mask: mask}" [(ngModel)]="myModel" type="text"/> ` }) export class AppComponent { public myModel = '' public mask = ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/] }
Documentation
As you can see in the code above, you are passing an object to the textMask
directive.