D
D
DTX2016-04-28 00:56:05
JavaScript
DTX, 2016-04-28 00:56:05

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

2 answer(s)
_
_ _, 2016-04-28
@DirecTwiX

@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; 
  }
}

And if you want reusability:
@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 = ''; 
  }
}

A
Alexander Plaksenko, 2016-04-28
@TrustInside

https://angular.io/docs/ts/latest/guide/attribute-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question