How to get ng-class value in directive

I have some duplicate "li" elements and some of them have an "active" class like this

<li ng-repeat="menuSubItem in menuItem.items" ng-class="{active: vm.currentMenuName == menuSubItem.name}" active-collapsible>

      

in hand, I have a directive to add some classes depending on the "active" class, my directive is like this

directive('activeCollapsible', function() {
  return {
    restrict: 'A',
    link: function($scope, element, attrs) {

    }
  }
});

      

but I don't see the "active" class in the "element" argument. (I am already adding jQuery before Angular.)

$(element).hasClass('active')

      

How can I get the "active" class in my directive?

+3


source to share


1 answer


You can get an element with an active class, or not use $watch

functino in scope, the return function in the $watch

first part will be evaluated every time the digest loop starts, and then you get either a true or a false value for basicselement.hasClasss('active')

Directive



directive('activeCollapsible', function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            $scope.$watch(function() {
                return element.hasClass('active')
            }, function(newVal) {
                if (newVal) //then it has active Class
                //do something
            });
        };
    };
});

      

+4


source







All Articles