Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. <ion-input type="tel" placeholder="celular" name="celular"
  2. [(ngModel)]="phone_number" required
  3. mask="(**)*****-****" ></ion-input>
  4.  
  5. #3 steps:
  6. # ====>1 - create a Directive.
  7. # ====>2 - import to the main module by placing in the declarations array.
  8. # ====>3 - use the directive in the html file.
  9.  
  10. #step1
  11. import { Directive, Attribute } from '@angular/core';
  12. import { NgModel } from '@angular/forms';
  13. @Directive({
  14. selector: '[mask]',
  15. host: {
  16. '(keyup)': 'onInputChange($event)'
  17. },
  18. providers: [NgModel]
  19. })
  20. export class Mask {
  21. maskPattern: string;
  22. placeHolderCounts: number;
  23. dividers: string[];
  24. modelValue: string;
  25. viewValue: string;
  26.  
  27. constructor(
  28. public model: NgModel,
  29. @Attribute("mask") maskPattern: string
  30. ) {
  31. this.dividers = maskPattern.replace(/*/g, "").split("");
  32. this.dividers.push(" ");
  33. this.generatePattern(maskPattern);
  34. }
  35.  
  36. onInputChange(event) {
  37. this.modelValue = this.getModelValue(event);
  38. let stringToFormat = this.modelValue;
  39. if (stringToFormat.length < 10) {
  40. stringToFormat = this.padString(stringToFormat);
  41. }
  42.  
  43. this.viewValue = this.format(stringToFormat);
  44. this.writeValue(event.target, this.viewValue);
  45. }
  46.  
  47. writeValue(target, value) {
  48. return target.value = value;
  49. }
  50.  
  51. generatePattern(patternString) {
  52. this.placeHolderCounts = (patternString.match(/*/g) || []).length;
  53. for (let i = 0; i < this.placeHolderCounts; i++) {
  54. patternString = patternString.replace('*', "{" + i + "}");
  55. }
  56. this.maskPattern = patternString;
  57. }
  58.  
  59. format(s) {
  60. var formattedString = this.maskPattern;
  61. for (let i = 0; i < this.placeHolderCounts; i++) {
  62. formattedString = formattedString.replace("{" + i + "}", s.charAt(i));
  63. }
  64. return formattedString;
  65. }
  66.  
  67. padString(s) {
  68. var pad = " ";
  69. return (s + pad).substring(0, pad.length);
  70. }
  71.  
  72. getModelValue(event) {
  73. var modelValue = event.target.value;
  74. for (var i = 0; i < this.dividers.length; i++) {
  75. while (modelValue.indexOf(this.dividers[i]) > -1) {
  76. modelValue = modelValue.replace(this.dividers[i], "");
  77. }
  78. }
  79. return modelValue;
  80. }
  81. }
  82.  
  83. #step2
  84. import { Mask } from'./Mask';
  85. @NgModule({
  86. declarations: [
  87. MyApp,
  88. HomePage,
  89. Mask
  90. ],
  91.  
  92. #step3
  93. <form>
  94. <ion-list>
  95. <ion-item>
  96. <ion-input type="text" mask="(**)****-****"></ion-input>
  97. </ion-item>
  98. </ion-list>
  99. <button ion-button block type="submit">Save</button>
  100. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement