gpt4 book ai didi

angular - nativescript-angular - KeyboardEvent 未定义

转载 作者:行者123 更新时间:2023-12-04 10:56:35 25 4
gpt4 key购买 nike

我正在尝试创建一个指令,它将小数点限制为最多两个。所以当用户输入一个十进制数时,他最多只能输入两个小数点。
它适用于 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

最佳答案

回答

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

关于angular - nativescript-angular - KeyboardEvent 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com