Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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 自定义指令输入返回错误角度5_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 自定义指令输入返回错误角度5

Javascript 自定义指令输入返回错误角度5,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个自定义指令,我正在这样设置 import { Directive, HostListener, ElementRef, Input } from '@angular/core'; @Directive({ selector: '[pieceRate]' }) export class PieceRateDirective { @HostListener('input', ['$event']) @Input() dollarMax: string; @I

我有一个自定义指令,我正在这样设置

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

@Directive({
    selector: '[pieceRate]'
})
export class PieceRateDirective {
    @HostListener('input', ['$event'])
    @Input() dollarMax: string;
    @Input() centMax: string;
    onkeydown(event: KeyboardEvent) {
      const input = event.target as HTMLInputElement;
      console.log(this.dollarMax);
      console.log(this.centMax);
  }
}
<input
   pieceRate
   [dollarMax]="rangeTo?.toString().split('.')[0]"
   [(ngModel)]="dollarPieceRate"
   type="number" >
我将其导入到共享模块中,该模块将被导入到我的主模块中,因此

共享模块

import { PieceRateDirective} from '@app/directives...'
...

@NgModule({
   imports..
   declarations: [
     PieceRateDirective
   ],
   exports: [
      PieceRateDirective
   ]
})
import { SharedModule } from '@app/shared.module';
...

@NgModule({
   imports: [
      SharedModule
   ]
})
...
主模块

import { PieceRateDirective} from '@app/directives...'
...

@NgModule({
   imports..
   declarations: [
     PieceRateDirective
   ],
   exports: [
      PieceRateDirective
   ]
})
import { SharedModule } from '@app/shared.module';
...

@NgModule({
   imports: [
      SharedModule
   ]
})
...
所以这一切看起来都很好,但当我真的尝试这样使用我的指令时

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

@Directive({
    selector: '[pieceRate]'
})
export class PieceRateDirective {
    @HostListener('input', ['$event'])
    @Input() dollarMax: string;
    @Input() centMax: string;
    onkeydown(event: KeyboardEvent) {
      const input = event.target as HTMLInputElement;
      console.log(this.dollarMax);
      console.log(this.centMax);
  }
}
<input
   pieceRate
   [dollarMax]="rangeTo?.toString().split('.')[0]"
   [(ngModel)]="dollarPieceRate"
   type="number" >

输入一个数字,我得到这个错误

TypeError:jit\u nodeValue\u 10(…)。dollarMax不是函数

我不确定是什么导致了这个问题


任何帮助都将不胜感激

您必须将主机侦听器应用于函数,而不是输入字符串

export class PieceRateDirective {
  @Input() dollarMax: string;
  @Input() centMax: string;

  @HostListener('input', ['$event'])
  onkeydown(event: KeyboardEvent) {
    const input = event.target as HTMLInputElement;
    console.log(this.dollarMax);
    console.log(this.centMax);
  }
}
decorator用于将处理程序与事件关联。在本例中,
onkeydown
的声明应紧跟在装饰程序之后:

export class PieceRateDirective {

  @Input() dollarMax: string;
  @Input() centMax: string;

  @HostListener('input', ['$event'])
  onkeydown(event: KeyboardEvent) {
    ...
  }
}

尝试将两行
@Input
移到
@HostListener
上方(后面应该紧跟
onKeyDown
)。grrrr我刚刚花了5分钟来计算lol。。。很高兴你修好了