Remove or add class in Angular

前端 未结 5 2045
感动是毒
感动是毒 2021-02-14 08:37

I have a list and the plugin (dragula) I used, adds certain CSS class on certain action. I am using Angular 5. I want to find out the presence of certain class (

5条回答
  •  旧巷少年郎
    2021-02-14 09:43

    Import ElementRef from angular core and define in constructor then try below code:

    Below line of code will give you first occurrence of

    tag from Component. querySelector gives you first item and querySelectorAll gives you all items from DOM.

    import { Component, ElementRef } from "@angular/core";
    
    constructor(private el: ElementRef) {
    }
    
    let myTag = this.el.nativeElement.querySelector("p"); // you can select html element by getelementsByClassName also, please use as per your requirement.
    

    Add Class:

    if(!myTag.classList.contains('myClass'))
    {
        myTag.classList.add('myClass'); 
    }
    

    Remove Class:

    myTag.classList.remove('myClass'); 
    

提交回复
热议问题