Angular 2 KeyboardEvent的按键不起作用

时间:2018-09-17 11:28:14

标签: angular typescript angular6 keyboard-events keydown

我正尝试启动一个keydown事件,该事件会将按下的keydown更改为逗号。如果按其他一些非数字值,它将发送一个模拟逗号的keydown事件,并且预期的行为是在文本框中写入逗号。问题是当它完成第二个调用(逗号一个)时,它没有写逗号。有人对此有想法吗?

谢谢。

<input type="text" (keydown)="validate($event)" />


validate(event) {

    if (!this.validatenumeric(event)) {
      event.preventDefault();

    } else {
        if(event.key!=","){
        event.preventDefault();
        var getterCode = { get: function () { return 188 } };
        var getsourceCapabilities = { get: function () { return event.sourceCapabilities } };
        var e = new KeyboardEvent("keydown", {
          altKey: false,
          bubbles: true,
          cancelable: true,
          code: "Comma",
          composed: true,
          ctrlKey: false,
          key: ",",
          detail: 0,
          location: 0,
          metaKey: false,
          repeat: false,
          shiftKey: false,
          view: window
        });

        Object.defineProperties(e, {

          which: getterCode,
          keyCode: getterCode,
          sourceCapabilities: getsourceCapabilities
        });

            this.elementRef.nativeElement.querySelector('input').dispatchEvent(e);

          }
        }
      }


 validatenumeric(key) {
    var keycode = (key.which) ? key.which : key.keyCode;
    //comparing pressed keycodes
    if ((keycode >= 48 && keycode <= 57)) {
      // 0-9 only
      return true;
    } else if ((keycode >= 96 && keycode <= 105)) {
      // 0-9 only
       return true;
    }
    else return false;

    return false;
  }

一些更新。。我想我可以解决这个问题。而是尝试执行另一次keydown。.我创建一个KeyUp,并检查它是否为逗号。.获取光标所在的位置,并将逗号插入到该位置。不知道是否有一种简单的方法:)谢谢大家

> <input type="text" (keydown)="validate($event)"
> (keyup)="validateUp($event)" />
> 
>   validateUp(event) {    let pos =
> this.elementRef.nativeElement.querySelector('input').selectionEnd;
>     if (event.key == ',' ) {
>       this.textValue = text.slice(0, pos) + event.key + text.slice(pos);
>       }   }
> 
> 
>   validateWithtDecimals(event) {
>     var text: string = this.textValue;
> 
> 
>     if (!this.validatenumeric(event, text, this.separator)) {
>       event.preventDefault();
>     }
>     else {
> 
>        if(event.key!=","){
>         event.preventDefault(); 
>       
>         var getterCode = { get: function () { return 188 } };
> 
>         var e = new KeyboardEvent("keyup", {
>           altKey: false,
>           bubbles: true,
>           cancelable: true,
>           code: "Comma",
>           composed: true,
>           ctrlKey: false,
>           key: ",",
>           detail: 0,
>           location: 0,
>           metaKey: false,
>           repeat: false,
>           shiftKey: false,
>           view: window
>         });
> 
>         Object.defineProperties(e, {
>           which: getterCode,
>           keyCode: getterCode,
>         });
> 
>         this.elementRef.nativeElement.querySelector('input').dispatchEvent(e);
>       }
>     }   }

1 个答案:

答案 0 :(得分:0)

我建议使用绑定而不是键盘事件:

 <input type="text" [(ngModel)]="valueToValidate" />

component.ts 中:

 private _value = '';
 get valueToValidate(): string {
     return this._value;
 }
 set valueToValidate(value: string) {
     value = value.replace('.',',');
     // your validation here
     this._value = value;
 }