如果用户未登录,我正在尝试以重定向到登录页面的方式配置我的路由。这是我的代码:
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 没有定义,所以我得到了错误。 我试图像这样注入(inject) $location 作为依赖项,但我得到了未定义的注入(inject)(injpt):
.run(['$rootScope', 'checkLogin', '$location', function($rootScope, checkLogin ,$location) {
$rootScope.$on('$routeChangeSuccess', function () {
if (checkLogin())
$location.url("/ciao");
})
}])
所以我想知道如何在我的“运行”方法中使用内置的 $location 服务...为什么我可以将它作为 $rootScope 或 checkLogin 的依赖项注入(inject)?!
我使用的是 angular 1.2.0 rc2。
谢谢
最佳答案
这是一个工作示例 http://plnkr.co/edit/gf5LhTjqhz4MoZfVcqIt?p=preview
无法重现 $location 在 .run 中不起作用的错误
关于javascript - Angular JS : $location injection undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670220/