简体   繁体   中英

ng-class with condition and function in one ng-class directive

I want ng-class to have a condition and a function call. How do I make the changes necessary to:

<li ng-repeat="t in tabs" ng-class="{'active1':t.active,clickmenu(t.route)}" ng-click='clickMainMenu($index,t)'><div class='custom-menu-li' >{{t.heading | translate }}</div>       
</li>

so as to make ng-class have a condition and a function in one ng-class directive.

controller.js

$scope.isActive = function(t) {
    if (t.active) {
       clickmenu(t.route);
       return true;
    }
}

template.html

<li ng-repeat="t in tabs" 
    ng-class="{'active1': isActive(t)}" 
    ng-click='clickMainMenu($index,t)'>
  <div class='custom-menu-li' >{{t.heading | translate }}</div> 
</li>

NOTE: This is not the ideal thing to do . Your clickmenu will get called for every digest cycle.

You can do like this using && :

<li ng-repeat="t in tabs" ng-class="{'active1':t.active && clickmenu(t.route)}" ng-click='clickMainMenu($index,t)'><div class='custom-menu-li' >{{t.heading | translate }}</div>       
</li>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM