如何为输入创建蒙版?

时间:2016-10-08 16:50:57

标签: angular

如何使用Angular 2为input创建蒙版?

我尝试使用PrimeNG,但属性required无效。 我也试过angular2-text-mask,但它也不起作用。

有什么建议吗?

更新

我使用了这个库:https://github.com/text-mask/text-mask/tree/master/angular2

  1. 首先,我安装了:npm i angular2-text-mask --save

  2. 然后,我包含在模块中:

  3. 
    
    import MaskedInput  from 'angular2-text-mask'
    
    @NgModule({
      imports:      [ FormsModule, HttpModule ],
      declarations: [ MaskedInput ],
      providers:    [ appRoutingProviders ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
    
    
    

    1. 然后,我加入了我的组件:
    2. 
      
      @Component({
          selector: 'my-app',
          providers: [LojaService],
          templateUrl: 'app/admin/loja/loja-form/form.html'
      
      })
      
      export class FormComponent { 
          public mask = ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
      
        ...
      }
      
      
      

      1. 我的form.html
      2. 
        
        <form (ngSubmit)="onSubmit(f)" #f="ngForm">
        ...
        
          <input [textMask]="{mask: mask}" [(ngModel)]="myModel" type="text"/>
          
        ...
        </form>
        &#13;
        &#13;
        &#13;

        1. 结果:
        2. enter image description here

          **整理出来! **

          我使用了另一个解决方案:一个插件蒙面jQuery。我完成了插件与Angular 2的集成。

1 个答案:

答案 0 :(得分:0)

根据他们的文档。 您应该导入TextMaskModule而不是MaskedInput

至少现在这样做了。

相关问题