简体   繁体   English

带有主机监听器的 Angular 中的活动 class

[英]Active class in Angular with hostlistener

how to remove active class on li tag?如何删除 li 标签上的活动 class? I have a code that adds an active class on a li tag but I can't remove this active class我有一个代码在 li 标签上添加了一个活动 class 但我无法删除这个活动 class

   @Directive({
  selector: '[appHome]'
  
})
export class HomeDirective{
  ref!: ElementRef;

  @HostBinding('class.active')isActive=false;



  @HostListener('click')onClick(){
    
    if(!this.isActive){
      this.isActive=true;
      return;
    }
    
    }
    
            <p appHome> {{wd.titleName}}</p>
            <!-- <span class="line-performance"></span> -->
        </li>



    </ul>

I think what you are looking for is a toggle behavior.我认为您正在寻找的是切换行为。 If it's so replace the following如果是这样替换以下

if(!this.isActive){
  this.isActive=true;
  return;
}

with

this.isActive = !this.isActive;

If you want to add the active class conditionally you can use ngClass on the list item as follows如果要有条件地添加活动 class 可以在列表项上使用ngClass ,如下所示

<li [ngClass]="{'active' : isActive == true}">List Item</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM