href指令在Angular中不起作用
我已设置以下指令以捕获所有href指令在Angular中不起作用,angular,directive,Angular,Directive,我已设置以下指令以捕获所有hrefs上的点击: import {Directive, ElementRef, Input} from '@angular/core'; @Directive({ selector : '[href]', host : { '(click)' : 'doNothing($event)' } }) export class HrefTelDirective { @Input() href: string;
href
s上的点击:
import {Directive, ElementRef, Input} from '@angular/core';
@Directive({
selector : '[href]',
host : {
'(click)' : 'doNothing($event)'
}
})
export class HrefTelDirective {
@Input() href: string;
constructor(el: ElementRef) {
console.log('href loaded');
console.log(el);
}
doNothing(event) {
// if(this.href.length === 0 || this.href === '#') {
// event.preventDefault();
// }
console.log('href clicked');
}
}
我将指令加载到不同的模块上,包括core、shared等,但从未调用它。当我将选择器更改为其他名称(如“myHighlightDirective”)时,它将被调用 不知道。代码看起来很好…可能是导入问题。对我来说很好:检查这个