How would I go about using NgClass inside of a custom attribute directive to change the main elements CSS class?
If I have something like this:
@Component({
selector: 'my-app',
template: `
<div>
<div class="box" myDir [ngClass]="{'blue': blue, 'red': red}"> </div>
</div>
`,
});
And then, inside of that myDir directive, something like this:
import { Directive, HostListener, OnInit } from '@angular/core';
@Directive({
selector: '[myDir]'
})
export class MyDirDirective {
blue: boolean;
red: boolean;
constructor() {
}
ngOnInit() {
}
@HostListener('mouseenter', ['$event'])
onMouseEnter(event) {
event.preventDefault();
event.stopPropagation();
this.blue = true;
this.red = false;
console.log('mouseenter');
}
@HostListener('mouseleave', ['$event'])
onMouseLeave(event) {
event.preventDefault();
event.stopPropagation();
this.blue = true;
this.red = false;
console.log('mouseleave');
}
Do I not have access to the scope in which blue and red reside in? If I create a toggle, I can update those values with a button, but it doesn't seem like I can do it from within the directive itself. Is this an accurate observation and should I be doing it this way or is there an alternative that I'm not seeing in the docs?