auth is undefined - ui-router & Auth0

2019-09-03 12:55发布

问题:

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?

!-- controller

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');
                }
            }
        }

    });

});

回答1:

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) {
..}]);