Ionic 4
Can i use this ngx-mask with ionic 4 & Angular 7 ? Thanks
@blondie63 yes
any example for that?
I have the solution for working in IONIC 4and Angular 7
use <input> instead of <ion-input>
And make input with SCSS look like ion-input.
Use NgxMaskModule.forRoot(),
page.module.ts (page is the page name where you want to use the mask).
@mihirp1730
use <input> instead of <ion-input>
That's not a solution.
Please share the solution. So we can use instead of this.
Please share the solution. So we can use instead of this.
On app.module:
imports:[
...
NgxMaskModule.forRoot()
...
]
On yourcomponent.module:
imports:[
...
NgxMaskModule.forChild()
...
]
And finally to use on your html:
<ion-input (ngModelChange)="user.cpf=$event" [ngModel]="user.cpf | mask: '000.000.000-99'"></ion-input>
<span>Typed: {{user.cpf | mask: '000.000.000-99'}}<span>
I figure out a small solution, you guys can implement inside your needs.
First step: Create a directive to take the mask from ion-input and apply in it self.
import { Directive, Input } from '@angular/core';
import { IonInput } from '@ionic/angular';
import { MaskApplierService } from 'ngx-mask';
@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
selector: 'ion-input'
})
export class CustomMaskDirective {
@Input() mask: any;
constructor(
private control: IonInput,
private maskService: MaskApplierService,
) {
this.control.ionChange.subscribe((r: string) => {
this.control.value = this.maskService.applyMask(this.control.value.toString(), this.mask);
});
}
}
Second step: Import this directive into your App.Module.ts or Some module you are using.
import { IConfig, NgxMaskModule } from 'ngx-mask';
import { CustomMaskDirective } from './shared/directives/custom-mask.directive';
// ?Modulo principal da aplicação
@NgModule({
declarations: [
AppComponent,
CustomMaskDirective // this is your directive
],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
HttpClientModule,
NgxMaskModule.forRoot()
],
providers: [
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
{ provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true },
],
bootstrap: [AppComponent],
})
export class AppModule {
}
After these steps, the mask will be applied to your ion-input.
obs: Is not a full solution, but you can work with ^ ^
I figure out a small solution, you guys can implement inside your needs.
First step: Create a directive to take the mask from ion-input and apply in it self.
import { Directive, Input } from '@angular/core'; import { IonInput } from '@ionic/angular'; import { MaskApplierService } from 'ngx-mask'; @Directive({ // eslint-disable-next-line @angular-eslint/directive-selector selector: 'ion-input' }) export class CustomMaskDirective { @Input() mask: any; constructor( private control: IonInput, private maskService: MaskApplierService, ) { this.control.ionChange.subscribe((r: string) => { this.control.value = this.maskService.applyMask(this.control.value.toString(), this.mask); }); } }Second step: Import this directive into your App.Module.ts or Some module you are using.
import { IConfig, NgxMaskModule } from 'ngx-mask'; import { CustomMaskDirective } from './shared/directives/custom-mask.directive'; // ?Modulo principal da aplicação @NgModule({ declarations: [ AppComponent, CustomMaskDirective // this is your directive ], entryComponents: [], imports: [ BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpClientModule, NgxMaskModule.forRoot() ], providers: [ { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, { provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true }, ], bootstrap: [AppComponent], }) export class AppModule { }After these steps, the mask will be applied to your ion-input.
obs: Is not a full solution, but you can work with ^ ^
Many thanks! This worked for me in ionic 5.
I figure out a small solution, you guys can implement inside your needs.
First step: Create a directive to take the mask from ion-input and apply in it self.
import { Directive, Input } from '@angular/core'; import { IonInput } from '@ionic/angular'; import { MaskApplierService } from 'ngx-mask'; @Directive({ // eslint-disable-next-line @angular-eslint/directive-selector selector: 'ion-input' }) export class CustomMaskDirective { @Input() mask: any; constructor( private control: IonInput, private maskService: MaskApplierService, ) { this.control.ionChange.subscribe((r: string) => { this.control.value = this.maskService.applyMask(this.control.value.toString(), this.mask); }); } }Second step: Import this directive into your App.Module.ts or Some module you are using.
import { IConfig, NgxMaskModule } from 'ngx-mask'; import { CustomMaskDirective } from './shared/directives/custom-mask.directive'; // ?Modulo principal da aplicação @NgModule({ declarations: [ AppComponent, CustomMaskDirective // this is your directive ], entryComponents: [], imports: [ BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpClientModule, NgxMaskModule.forRoot() ], providers: [ { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, { provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true }, ], bootstrap: [AppComponent], }) export class AppModule { }After these steps, the mask will be applied to your ion-input.
obs: Is not a full solution, but you can work with ^ ^
Works with Ionic 6 too
I had to tweak @kevingamaa 's code a little because for some reason that I could not figure out the original code was breaking during Cypress tests.
constructor(private control: IonInput, private maskService: NgxMaskApplierService) {
this.control.ionChange.subscribe((r: string) => {
if (this.mask) {
this.control.value = this.maskService.applyMask(this.control.value.toString(), this.mask);
}
});
}
I just added the if (this.mask) check
Anybody know how to add something like suffix or prefix this way?
I don't know if something changed internally with the ion-inputs in version 8, but the masks were only being applied after moving the focus away from the input.
So I changed the code a little, and now it works as expected:
import { ChangeDetectorRef, Directive, input } from '@angular/core';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
import { IonInput } from '@ionic/angular/standalone';
import { NgxMaskService } from 'ngx-mask';
import {
combineLatest,
filter,
from,
fromEvent,
map,
NEVER,
switchMap,
tap,
} from 'rxjs';
@Directive({
selector: 'ion-input[mask]',
standalone: true,
})
export class MaskDirective {
readonly mask = input<any>();
constructor(
ionInput: IonInput,
maskService: NgxMaskService,
cdRef: ChangeDetectorRef
) {
/**
* Need to reattach here due to the following issue:
* https://github.com/angular/angular/issues/55644
*/
cdRef.reattach();
combineLatest([
from(ionInput.getInputElement()),
toObservable(this.mask),
])
.pipe(
switchMap(([inputEl, mask]) => {
if (inputEl == null || mask == null) {
return NEVER;
}
return fromEvent<KeyboardEvent>(inputEl, 'keyup').pipe(
map((ev) => (ev.target as HTMLInputElement).value),
filter((value) => !!value),
tap(
(value) =>
(ionInput.value = maskService.applyMask(
value.toString(),
this.mask()
))
)
);
}),
takeUntilDestroyed()
)
.subscribe();
}
}