@Component({
selector: 'mouse-hover-test',
template: '<div [class.hovered]="hovered"></div>"'
})
class MouseHoverTestCmp {
public hovered: boolean;
@HostListener('mouseover') onMouseOver() {
this.hovered = true;
}
@HostListener('mouseout') onMouseOut() {
this.hovered = false;
}
}
А если реюзабельности захочется:
@Directive({
selector: '[hoverClass]'
})
class HoverClassCmp {
@Input('hoverClass') className: string;
@HostBinding('class') activeClass: string;
@HostListener('mouseover') onMouseOver() {
this.activeClass = this.className;
}
@HostListener('mouseout') onMouseOut() {
this.activeClass = '';
}
}