nativescript-angular-KeyboardEvent未定义

我正在尝试创建一个指令,该指令将小数点数限制为最多两个。因此,当用户输入一个十进制数字时,他最多只能输入两个小数点。 它适用于Angular,但不适用于Nativescript Angular。

这是我创建的指令:

import { Directive,ElementRef,HostListener,Input } from '@angular/core';

@Directive({
  selector: '[nsTwoDecimalPlaceLimit]'
})
export class TwoDecimalPlaceLimitDirective {

  private regex: RegExp = new RegExp(/^\d*\.?\d{0,2}$/g);
  private specialKeys: Array<string> = ['Backspace','Tab','End','Home','-','ArrowLeft','ArrowRight','Del','Delete'];

  constructor(private el: ElementRef) {
  }

  @HostListener('keydown',['$event'])
  onKeyDown(event: KeyboardEvent) {
    // Allow Backspace,tab,end,and home keys
    if (this.specialKeys.indexOf(event.key) !== -1) {
      return;
    }
    let current: string = this.el.nativeElement.value;
    const position = this.el.nativeElement.selectionStart;
    const next: string = [current.slice(0,position),event.key == 'Decimal' ? '.' : event.key,current.slice(position)].join('');
    if (next && !String(next).match(this.regex)) {
      event.preventDefault();
    }
  }
}

HTML部分如下所示:

    <StackLayout class="form">
        <TextField class="m-5 input input-border" hint="Disabled" nsTwoDecimalPlaceLimit>
        </TextField>
    </StackLayout>

运行此命令时,出现以下错误:

ERROR Error: Uncaught (in promise): ReferenceError: KeyboardEvent is not defined

这里是 Playground Sample

kyj09 回答:nativescript-angular-KeyboardEvent未定义

管理答案中

https://github.com/angular/universal/issues/830#issuecomment-345228799

您必须更改node_modules

angular-universal-starter/blob/master/server.ts

替换

global['KeyboardEvent'] = win.Event;

也请检查

Angular Universal ReferenceError - KeyboardEvent is not defined

本文链接:https://www.f2er.com/2994157.html

大家都在问