I have a problem with attribute directive. I've followed the tutorial.
Directive is generated using CLI, so I've used ng g directive <directivename>
, and is purposely put in the top level, altogether with app.module.ts
.
My app.module.ts
looks like this (and I must have omitted all the imports due to proprietary names of the modules):
// Directives
import { EventhoverDirective } from './eventhover.directive';
@NgModule({
declarations: [
// all the relevant component inputs are here
EventhoverDirective
],
imports: [
// modules are here
],
providers: [
// providers are here
],
bootstrap: [AppComponent]
})
export class AppModule { }
Directive itself looks like:
import { Directive, HostListener, OnInit, Renderer2, ElementRef } from '@angular/core';
@Directive({
selector: '[appEventhover]'
})
export class EventhoverDirective implements OnInit {
constructor(private el: ElementRef, private renderer: Renderer2) { }
ngOnInit() {
console.log('Directive called');
}
@HostListener('mouseenter') onMouseEnter() {
this.highlight('blue');
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight(null);
}
highlight(color: string) {
this.renderer.setStyle(this.el.nativeElement, 'color', color);
}
}
And I use it inside the HTML like this:
<div class="container top-spacer">
<div class="row text-center" >
<div appEventhover class="col event" *ngFor="let event of eventList" (click)="storeEvent(event.job)">
<img class="img-responsive" src="{{backendRoute}}/{{event.track_image_off}}">
<p > {{event.name}} </p>
</div>
</div>
</div>
However, it's not working. It doesn't even spit out any error, nor anything else. What I might be doing wrong?
Thank you in advance for your help.
{{event.name}}
– rmcsharry Jun 12 '18 at 15:20