Issue
Here is the directive, the default one.
import { Directive, Input, Renderer2, ElementRef } from '@angular/core';
@Directive({
selector: '[newBox]'
})
export class BoxDirective {
@Input() backgroundColor = '#fff';
constructor(private el: ElementRef, private renderer: Renderer2) {}
ngOnInit(): void {
this.renderer.setStyle(this.el.nativeElement, 'background-color', this.backgroundColor);
}
}
Now, on hover, I want the background-color to change. How do I do that in the directive?
Solution
use HostListener
to listen to events
@HostListener('mouseover')
onMouseOver() {
this.backgroundColor = '#fff';
}
@HostListener('mouseout')
onMouseOut() {
this.backgroundColor = '#000';
}
Answered By - Sachila Ranawaka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.