我创建了指令。
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[appHover]'
})
export class HoverDirective {
@HostBinding( 'class.under' ) isunder = false;
@HostListener( 'mouseenter' ) OnEnter(){
this.isunder = true
}
@HostListener( 'mouseleave' ) OnLeave(){
this.isunder = false
}
}
将鼠标悬停在第一个导航上时
<nav appHover> nav 1</nav>
<nav> nav 2</nav>
样式应用于它
.under
text-decoration: underline
如何将悬停在第一个导航上应用样式到第二个导航?
你可以使用
native APItypedocument.querySelector.classList.toggle,但这不是 Angular 的方式。只需在第一个上放置一个事件处理程序nav并将第二个声明nav为模板变量:在组件中: