Limit the length of input field using angular2

后端 未结 3 772
[愿得一人]
[愿得一人] 2021-01-14 03:57

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.

component

3条回答
  •  感动是毒
    2021-01-14 04:40

    Please refer

    import { Directive, Input, Output, EventEmitter } from '@angular/core';
    
    @Directive({
     selector: '[limit-to]',
     host: {
    '(input)': 'onInputChange($event)',
    }
    })
    export class LimitToDirective {
     @Input('limit-to') limitTo;
     @Output() ngModelChange: EventEmitter = new EventEmitter();
     oldValue: any;
    
     onInputChange(e) {
    debugger
    const limit = +this.limitTo;
    if (e.target.value.length > limit) {
      e.target.value = this.oldValue;
    }
    this.oldValue = e.target.value;
    this.ngModelChange.emit(e.target.value);
    }
    }
    
    
    
    

    click

提交回复
热议问题