使用angular2限制输入字段的长度 [英] Limit the length of input field using angular2

查看:100
本文介绍了使用angular2限制输入字段的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已实现指令以使用angular2限制输入字段.在桌面浏览器上可以正常工作,但在android mobile上不能工作.

I have implemented the directive to restrict the input field using angular2. It is working fine in desktop browser, but not working in android mobile.

import { LimitToDirective } from '../../../directives/limitedvalidation';
@Component({
  templateUrl: 'build/pages/profile/change-basic-details/change-basic-details.html',
  directives: [LimitToDirective]  
})
export class UpdateBasicDetailsPage {
  constructor(){}
}

指令

 import {Directive, Input} from '@angular/core'
    @Directive({
      selector: '[limit-to]',
      host: {
        '(keypress)': '_onKeypress($event)',
      }
    })
    export class LimitToDirective {
      @Input('limit-to') limitTo; 
      _onKeypress(e) {
         const limit = +this.limitTo;
         if (e.target.value.length === limit) e.preventDefault();
      }
    }

模板

<ion-input limit-to="12" type="tel" [formControl]="aadhaarno">

推荐答案

使用maxlength如下:

Use maxlength as belows :

<ion-input [maxlength]="12" type="tel" [formControl]="aadhaarno">

这篇关于使用angular2限制输入字段的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆