How to add and remove class with AngularJS?

前端 未结 2 2000
一个人的身影
一个人的身影 2021-01-12 16:08

I have a few buttons that work like switchers. If you click one it becomes active and \"shuts down\" other buttons. I did this using jQuery but would like to use AngularJS.

2条回答
  •  北海茫月
    2021-01-12 17:07

    You can use

    angular.element(document.querySelector("#cntrlID")).removeClass("customclass");

    HTML:

    
    

    Angular

    $scope.removeNS = function(){
        angular.element(document.querySelector("#normal")).removeClass("active");
        angular.element(document.querySelector("#strong")).removeClass("active");
    }
    $scope.removeWS = function(){
        angular.element(document.querySelector("#weak")).removeClass("active");
        angular.element(document.querySelector("#strong")).removeClass("active");
    }
    $scope.removeWN = function(){
        angular.element(document.querySelector("#weak")).removeClass("active");
        angular.element(document.querySelector("#normal")).removeClass("active");
    }
    

    Further to optimize, you can just create a single function and pass the query selectors and class to remove as the function parameter, like:

    function(id1,id2,removeClassName)
    

提交回复
热议问题