ngx-mask icon indicating copy to clipboard operation
ngx-mask copied to clipboard

Ionic 4

Open blondie63 opened this issue 7 years ago • 12 comments

Can i use this ngx-mask with ionic 4 & Angular 7 ? Thanks

blondie63 avatar Mar 06 '19 08:03 blondie63

@blondie63 yes

Thegrep01 avatar Mar 11 '19 14:03 Thegrep01

any example for that?

damsorian avatar Apr 17 '19 02:04 damsorian

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 avatar Jun 12 '19 13:06 mihirp1730

@mihirp1730

use <input> instead of <ion-input>

That's not a solution.

zeromonad avatar Jun 20 '19 18:06 zeromonad

Please share the solution. So we can use instead of this.

mihirp1730 avatar Jun 21 '19 09:06 mihirp1730

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>

gdinn avatar May 27 '20 05:05 gdinn

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 ^ ^

kevingamaa avatar May 17 '21 16:05 kevingamaa

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.

delisa-glover avatar Nov 23 '21 03:11 delisa-glover

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

Rickedb avatar Sep 23 '22 01:09 Rickedb

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

guaycuru avatar Feb 17 '23 17:02 guaycuru

Anybody know how to add something like suffix or prefix this way?

kyleabens avatar May 11 '23 17:05 kyleabens

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();
  }
}

188599 avatar Oct 10 '24 13:10 188599