我的指令有两个选择器 dirA
和 dirNotA
。该指令应根据使用的选择器进一步处理。有没有办法确定指令中使用了哪个选择器?
我不希望有多个指令,也不想有一个带参数的指令。我希望有一个带有多个选择器的指令,并根据模板中使用的选择器确定操作过程。
像这样
@Directive({
selector: '[dirA], [dirNotA]`
})
class DirectiveA implement OnInit {
ngOnInit() {
// here we detected which selector was used
if (dirASelector) {
...
}
}
}
有什么想法可以在指令本身中获取这些信息吗?
最佳答案
您可以使用继承。
class DirectiveAOrNotA implements OnInit {
// common logic here
}
@Directive({
selector: '[dirA]`
})
export class DirectiveA extends DirectiveAOrNotA {
// differences here
}
@Directive({
selector: '[dirNotA]`
})
export class DirectiveNotA extends DirectiveAOrNotA {
// differences here
}
关于angular - 多指令选择器 - 查找模板中使用了哪个选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60390879/