简体   繁体   中英

Active class in Angular with hostlistener

how to remove active class on li tag? I have a code that adds an active class on a li tag but I can't remove this active 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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