This Angular 2 Directive is also compatible with Ionic 2 ion-input
.
First, install it.
npm i angular2-text-mask --save
Then, import it into your @NgModule
and declare it:
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import MaskedInput from 'angular2-text-mask'
@NgModule({
imports: [FormsModule],
declarations: [MaskedInput]
})
export class TextMaskModule {}
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/]
}
As you can see in the code above, you are passing an object to the textMask
directive.
📍 For more information about the values that the textMask
object accepts, see
this page.
Text Mask does not provide an option to unmask the model before storing it. You can sanitize the model on your side. See here for details.
To see an example of the code running, follow these steps:
- Clone the repo,
git clone [email protected]:text-mask/text-mask.git
cd text-mask
npm install
npm run angular2:dev
- Open http://localhost:3000
The code of the example is in angular2/example
.
We would love some contributions! Check out this document to get started.