I have two selectors dirA and dirNotA for my directive. The directive should proceed further based on the selector used. Is there any way to determine which selector was used within the directive?
I do not wish to have multiple directives nor a directive with parameters. I wish to have one directive with multiple selectors and to determine the course of action based on the selector used in the template.
Something like this
@Directive({
selector: '[dirA], [dirNotA]`
})
class DirectiveA implement OnInit {
ngOnInit() {
// here we detected which selector was used
if (dirASelector) {
...
}
}
}
Any ideas how can get this information in the directive itself?
You can use inheritance.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With