问题
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