How to use addClass method in angularjs


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


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




 <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

Problem courtesy of: babtech


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


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.

Solution courtesy of: Shreyance Jain


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

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

Discussion courtesy of: Cybercarnage シ

try this

 link: function (lEmel) {         

Discussion courtesy of: user3597009

This recipe can be found in it's original form on Stack Over Flow.