$ location中没有定义$ location

时间:2018-02-12 11:16:50

标签: angularjs

在下面的,每当调用onSelect函数时,role.js文件,如果角色是UserRole,我将用户导航到不同的页面

angular.module('myApp.roles', ['ngRoute'])
       .config(['$routeProvider', function ($routeProvider) {
           $routeProvider.when('/roles', {
           templateUrl: 'roles/roles.html',
           controller: 'PostsCtrl',
           selector: 'roles'
       });
       $routeProvider.when('/footer', {
           templateUrl: 'footer/footer.html',
           controller: 'footerCtrl'
       });
      }])

      .controller('PostsCtrl', ['$scope', '$rootScope', '$location', function ($scope,
        $rootScope, $log, $location) {
           $scope.onSelect = function (A, B, C) {
               localStorage.setItem("A", JSON.stringify(A));
               localStorage.setItem("B", JSON.stringify(B));
               localStorage.setItem("C", JSON.stringify(C));
               if (role.roleName === "User Role") {
                   $rootScope.roles = [];
                   $location.url("/footer");
               } else {
                   $rootScope.mangoes = JSON.parse(localStorage.getItem("B"));
        }
    };
}]);

我在控制器中定义了$location并将其作为参数添加到函数中。当我调试它时,我发现$locationundefined。错误是:无法读取未定义的属性'url'。

1 个答案:

答案 0 :(得分:1)

您在注入参数时缺少订单,请从参数中删除 $log

.controller('PostsCtrl', ['$scope', '$rootScope', '$location' ,function ($scope,
 $rootScope, $location) {