Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么必须将指令添加到HTML标记中?_Javascript_Html_Angular - Fatal编程技术网

Javascript 为什么必须将指令添加到HTML标记中?

Javascript 为什么必须将指令添加到HTML标记中?,javascript,html,angular,Javascript,Html,Angular,请参阅以下我的指示: import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from "@angular/core"; @Directive({ selector: '[text]' }) export class TextDirective { @HostListener('document:keydown', ['$event']) onKeydownHandler(event) {

请参阅以下我的指示:

import { Directive, ElementRef, HostListener, Input, Output, EventEmitter } from "@angular/core";

@Directive({
  selector: '[text]'
})
export class TextDirective {

  @HostListener('document:keydown', ['$event']) onKeydownHandler(event) {

      var inputValue = event.which;
      if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0) && inputValue != 45 && inputValue != 8 && inputValue != 46) //{ //https://css-tricks.com/snippets/javascript/javascript-keycodes/
        event.preventDefault();   
  }
}
import{Directive,ElementRef,HostListener,Input,Output,EventEmitter}来自“@angular/core”;
@指示({
选择器:“[text]”
})
导出类TextDirective{
@HostListener('document:keydown',['$event'])onKeydownHandler(event){
var inputValue=event.which;

如果(!(inputValue>=65&&inputValue,则会发生这种情况,因为您绑定到整个文档上的keydown事件。 只需从
@HostListener

指令代码如下所示

@Directive({
  selector: '[text]'
})
export class TextDirective {

  @HostListener('keydown', ['$event']) onKeydownHandler(event) {
    // The function code
  }
}

您是否尝试从
@HostListener()中删除关键字文档
decorator?谢谢。JavaScript中有没有办法检测输入框中的文本是否被覆盖?我正在尝试在按键下进行更改后获取潜在值。如果我理解您的意思,我会想到唯一的方法。保存值的长度,然后检查它是高还是低。但是按键下的长度是更改前的长度?更改后如何获取该值。我昨天问了一个有关该值的问题,您是否可以回答:您是否可以使用keypress来代替?keypress无法检测何时按下删除键,因此我有相同的问题。
<input class="form-control form-input" type="number" placeholder="Age" [formControl]="newPersonForm.controls['age']">
@Directive({
  selector: '[text]'
})
export class TextDirective {

  @HostListener('keydown', ['$event']) onKeydownHandler(event) {
    // The function code
  }
}