角度JS:$location注入未定义

Angular JS: $location injection undefined

本文关键字:注入 未定义 location JS 角度      更新时间:2023-09-26

我正在尝试以这样一种方式配置我的路由,以便在用户未登录时重定向到登录页面。这是我的代码:

angular.module('witBiz.services', []);
angular.module('witBiz.controllers', ['witBiz.services']);
var witBizModule = angular.module('witBiz', ['ngRoute' , 'witBiz.controllers', 'witBiz.services'   ]);
witBizModule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/login', {templateUrl: 'resources/views/login.html', controller: 'LoginController'});
    $routeProvider.when('/index', {templateUrl: 'resources/views/index.html', controller: 'IndexController'});
    $routeProvider.otherwise({redirectTo: 'resources/views/index.html'});
}])
    .run(['$rootScope',  'checkLogin', function($rootScope, checkLogin ,$routeProvider ) {
        $rootScope.$on('$routeChangeSuccess', function () {
           if (checkLogin())
                $location.url("/index");
        })
    }])
    .factory('checkLogin', function(){
        return function() {
            //perform real logic here
            return true;
        }
    })

基本上我声明模块和服务。现在问题是$location没有定义,所以我得到错误。我试图像这样将$location作为依赖项注入,但我得到了未定义的注入(injpt):

.run(['$rootScope',  'checkLogin', '$location', function($rootScope, checkLogin ,$location) {
        $rootScope.$on('$routeChangeSuccess', function () {
           if (checkLogin())
                $location.url("/ciao");
        })
    }])

所以我想知道如何在我的"运行"方法中使用内置的$location服务......为什么我可以将其作为依赖项注入为$rootScope或检查登录?!

我正在使用角度 1.2.0 rc2。

谢谢

这是一个工作示例 http://plnkr.co/edit/gf5LhTjqhz4MoZfVcqIt?p=preview

无法重现错误,$location在 .run 中不起作用