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。。。很高兴你修好了