Suppose I have two directives:
@Directive({
selector: '[appDirective1]'
})
export class Directive1Directive {
public alone = true;
constructor(private element: ElementRef<HTMLInputElement>) { }
ngOnInit() {
if (this.alone) {
this.element.nativeElement.innerHTML = "I am alone";
} else {
this.element.nativeElement.innerHTML = "I have a friend";
}
}
}
@Directive({
selector: '[appDirective2]'
})
export class Directive2Directive {
constructor() { }
}
I want Directive1
to change its behavior if Directive2
is present on the same element. For example, these should have different behavior.
<div appDirective1></div>
<div appDirective1 appDirective2></div>
Is it possible for me to do this without adding a service to facilitate communication*?
StackBlitz Example: https://stackblitz.com/edit/angular-bgqd15
*I would like to avoid creating a new service for this because that feels like overkill.
@Host
over@Self
(angular.io/api/core/Self)? I'm not super familiar with the difference, but my initial impression is that@Host
will cast a wider net – Elephant