有条件的ng-class和$位置路径Angular

时间:2015-01-22 08:41:33

标签: javascript angularjs

这是我的网站,例如:example.com
条件是:

我想在用户处于这些位置时添加课程:
第一个条件:example.com/# wihch是用户点击没有链接的东西,如href ="#"
 和
第二:example.com/Cartable

这是我的小代码:

$scope.isActive = function (route) {
        return route === $location.path();

    }    

HTML

ng-class="{cartable:isActive('/Cartable')}" 

它正在工作,但我怎么能添加两个网址,如下所示:

ng-class="{cartable:isActive('/Cartable', '/#')}"   

感谢

3 个答案:

答案 0 :(得分:0)

您有两种选择,可以通过添加"或者#34;多次调用isActive函数。条件如下:

ng-class="{cartable: isActive('/Cartable') || isActive('/#')}" 

或更改isActive函数以接受数组:

$scope.isActive = function (routes) {
    angular.forEach(routes, function(route){
        if(route === $location.path())
            return true; 
    });
}

答案 1 :(得分:0)

在控制器中,使用参数

$scope.isActive = function () {

        for (var index = 0; index < arguments.length; index++) {
            if (arguments[index] === $location.path()) {
                return true;
            }
        }
        return false;
    }  

答案 2 :(得分:0)

如果您使用的是ui-router,请执行以下操作:

li(ui-sref-active="cartable") a(ui-sref="routeName")

https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-sref-active