Adding multiple expressions using ng-class in AngularJS

為{幸葍}努か 提交于 2019-12-06 15:12:38

Making my comment an answer, Yes your issue is with the duplicate keys, you could just do:-

ng-class="{ active: isActive('/') || isActive('/main/')}"

Or probably better:-

Let your isActive class accept multiple arguments:-

$scope.isActive = function () {
    //Look for a match in the arguments array
    return [].indexOf.call(arguments, location.path()) > -1;
};

and use it as:-

ng-class="{ active: isActive('/', '/main/')}"

Shim support for indexOf for older browsers

Day late but hopefully worth something.

You could also write it using the or operator for one instance of the class.

ng-class="{ 'active': condition1 || condition2 }"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!