Answer the question
In order to leave comments, you need to log in
Angular2. How to add a class to a block on mousehover?
Didn't find anything easy on the internet.
Answer the question
In order to leave comments, you need to log in
@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 = '';
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question