Javascript 角度:位置注入未定义
我正在尝试配置我的路由,以便在用户未登录时重定向到登录页面。这是我的代码:Javascript 角度:位置注入未定义,javascript,angularjs,Javascript,Angularjs,我正在尝试配置我的路由,以便在用户未登录时重定向到登录页面。这是我的代码: angular.module('witBiz.services', []); angular.module('witBiz.controllers', ['witBiz.services']); var witBizModule = angular.module('witBiz', ['ngRoute' , 'witBiz.controllers', 'witBiz.services' ]); witBizModu
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”方法中使用内置的$location服务……为什么我可以将其作为依赖项注入$rootScope或checkLogin
我使用的是angular 1.2.0 rc2
谢谢这里是一个有效的例子
由于$location内部不工作,无法再现错误。运行我一直在努力理解
应用程序。运行
您的示例有助于这感觉很困难,但它起了作用,因此经过两天的努力,我将接受它作为有效答案。谢谢
.run(['$rootScope', 'checkLogin', '$location', function($rootScope, checkLogin ,$location) {
$rootScope.$on('$routeChangeSuccess', function () {
if (checkLogin())
$location.url("/ciao");
})
}])