Angularjs 基于状态的Angular-ng类

Angularjs 基于状态的Angular-ng类,angularjs,state,ng-class,Angularjs,State,Ng Class,我的代码不起作用的具体原因是什么 <li ng-class="{active : $state.includes('products')}"> 您需要通过控制器中的$injector获取$state提供程序(就像您自己注入$scope或服务一样) 编辑: 包括: angular.module('yourModule', []).controller('YourController',['$injector', funciton($injector){} ]); 然后

我的代码不起作用的具体原因是什么

<li ng-class="{active : $state.includes('products')}">

您需要通过控制器中的$injector获取$state提供程序(就像您自己注入$scope服务一样)

编辑:

包括:

 angular.module('yourModule', []).controller('YourController',['$injector',
     funciton($injector){}
 ]);
然后:

您现在可以访问$state。要获取当前状态的名称,只需使用:

 $state.current.name
例如:

 <li ng-class="{active : $state.current.name === 'products'}">

  • 您需要通过控制器中的$injector获取$state提供程序(就像您将自己$scope服务注入一样)

    编辑:

    包括:

     angular.module('yourModule', []).controller('YourController',['$injector',
         funciton($injector){}
     ]);
    
    然后:

    您现在可以访问$state。要获取当前状态的名称,只需使用:

     $state.current.name
    
    例如:

     <li ng-class="{active : $state.current.name === 'products'}">
    

  • 您忘记在
    $rootScope
    上设置
    $state
    $stateParams
    ,如果这样做,您可以继续使用html上的第一种方法:

    angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
    });
    
    这样,您就不需要在每个控制器上注入它们

    以下是参考资料:


    PS:我个人不推荐这种全局方法,但它有其优点

    您忘记在
    $rootScope
    上设置
    $state
    $stateParams
    ,如果这样做,您可以继续在html上使用第一种方法:

    angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
    });
    
    这样,您就不需要在每个控制器上注入它们

    以下是参考资料:


    PS:我个人不推荐这种全局方法,但它有它的优点

    你应该添加你的控制器。在控制器中有类似于:
    $scope.$state=$state;
    ?你确定你正确引用了你的州名吗?也许可以将你的路由器代码添加到question@MB:你也添加了路由器代码应该使用ui.router extensions,这是breadcrumb功能。检查breadcrumb是否包含产品。您应该添加控制器。在控制器中有类似以下内容:
    $scope.$state=$state;
    ?您确定您正确引用了州名吗?也许可以将您的路由器代码添加到question@MB:也增加了路由器代码你应该使用ui.router扩展,这是breadcrumb功能。检查breadcrumb是否包含产品