javascript - AngularJS - how to set css class for the custom directive? -
have tab
css class nav
html element i'm going use directive this:
<nav tab></nav>
it expected interpreted this:
<nav class="tab"> <a></a> <a></a> <a></a> </nav>
... , works expected except of issue cannot set css class top <nav>
element. of course, specify using ng-class
explicitly seems not great idea.
have heard .addclass()
option doesn't work @ all:
tab.directive('tab', function($compile) { return { restrict: 'a', templateurl: 'nav-tab.html', controller: function($http, $scope) { $http.get('/tab/list') .success(function(data) { $scope.tabs = data; }).error(function() { alert("error"); }); }, controlleras: 'tab', link: function(element, scope) { angular.element(element).addclass('tab'); $compile(element)(scope); } } });
how add css class top directive element without it's explicit specifying right @ element?
you have wrong sequence in link function
instead of
link: function(element, scope) {
change to
link: function(scope, element, attrs) {
then can directly element.addclass
jqlite
api has .addclass
method available on it.
Comments
Post a Comment