身份验证未定义 - ui-router &授权0
问题描述:
我将 auth0 与 ui-router 一起使用.加载默认路由后,我可以看到 auth 对象已定义并包含方法.当我尝试访问控制器内的值时出现未定义错误.将这个对象作为可重用对象与我的控制器共享的最佳方式是什么?
I'm using auth0 with ui-router. When the default route is loaded I can see the auth object is defined and contains methods. I get an undefined error when I try to access the value inside a controller. What's the best way of sharing this object with my controllers as as a reusable object?
!-- 控制器
module.controller('login', ['$scope', function($scope,auth) {
!---
}).run(function($rootScope, auth, store, jwtHelper, $state, $stateParams) {
$rootScope.$on('$locationChangeStart', function() {
if (!auth.isAuthenticated) {
var token = store.get('token');
if (token) {
if (!jwtHelper.isTokenExpired(token)) {
auth.authenticate(store.get('profile'), token);
} else {
$state.go('login');
}
}
}
});
});
答
当您使用显式注解(正如您应该的那样)时,您需要显式定义所有参数.因此,将 'auth'
添加到该列表中:
When you use explicit annotation (as you should), you need to explicitly define all the parameters. So, add 'auth'
to that list:
module.controller('login', ['$scope', 'auth', function($scope, auth) {
..}]);