How to use addClass method in angularjs

元气小坏坏 提交于 2019-12-10 10:38:15

问题


I have an angularjs directive restricted to class. How can I add this by using addClass method in angularjs

directive

app.directive('number', function() {
return {
    restrict: 'C',
    link: function () {         

    }
};

});

code

 <div id="test"></div>

I know it is possible by using jQuery like the following.

$( "#test" ).addClass( "number" );

Please help me in doing it by using angularjs


回答1:


You don't need to use .addClass() angular provide ng-class directive to add class conditionally.

ng-class="{number:[condition]}"

it will add class number whenever your condition return true

yes but your directive will not be used by this I misunderstood your question previously.




回答2:


You should add ng-class="yourClass" to element on view and add in your controller

$scope.yourClass = "your-new-class", not in directive




回答3:


try this

 link: function (lEmel) {         
      lElem.addClass("Foo");
    }


来源:https://stackoverflow.com/questions/19488293/how-to-use-addclass-method-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!