发现AngularJS循环依赖:$ http< - Auth< - AuthInterceptor< - $ http< - $ templateRequest< - $ compile

时间:2015-08-13 14:02:34

标签: javascript angularjs html5 authentication

我正在从一本书中学习Angular,并且在我的Chrome控制台中收到以下错误...

&#34;找到循环依赖项:$ http&lt; - Auth&lt; - AuthInterceptor&lt; - $ http&lt; - $ templateRequest&lt; - $ compile&#34; < /强>

这是来自相关文件的代码。非常感谢任何帮助,因为我是Angular的总菜鸟:(

的index.html

<!DOCTYPE html>
<html lang="en">
<head>

    <meta char-set="UTF-8">
    <!--FOR ANGULAR ROUTING-->
    <base href="/">
    <link rel="stylesheet" href="assets/css/style.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular-route.js"></script>

    <script src="app/controllers/mainCtrl.js"></script>
    <script src="app/controllers/userCtrl.js"></script>
    <script src="app/services/authService.js"></script>
    <script src="app/services/userService.js"></script>

    <script src="app/app.routes.js"></script>
    <script src="app/app.js"></script>

</head>

<body ng-app="userApp" ng-controller="mainController as main">

    <header>
        <ul>
            <li ng-if="!main.loggedIn">
                <a href="/login">Login!</a>
            </li>

            <li ng-if="main.loggedIn">
                Hello, {{main.user.name}}
            </li>

            <li ng-if="main.loggedIn">
                <a href="#" ng-click="main.doLogout()">Logout</a>
            </li>
        </ul>
    </header>

    <main>
        <div ng-view></div>
    </main>

</body>
</html>

app.js

angular.module('userApp', [
'app.routes',
'authService',
'mainCtrl',
'userCtrl',
'userService'
])

.config(function($httpProvider) {
    //Attach our auth interceptor to integrate token into request.
    $httpProvider.interceptors.push('AuthInterceptor');
});

authService.js

angular.module('authService',[])

.factory('Auth', function($http, AuthToken) {

var authFactory = {};

//Login
authFactory.login = function(username, password) {
    return $http.post('/login', {
        username: username,
        password: password
    })
    .success(function(data){
        AuthToken.setToken(data.token);
        return data;
    });
};

//Logout
authFactory.logout = function() {
    AuthToken.setToken();
};

//Check if user is logged in.
authFactory.isLoggedIn = function() {
    if (AuthToken.getToken())
        return true
    else
        return false

};

authFactory.getUser = function() {

    if (AuthToken.getToken())
        return $http.get('/api/users/me', { cache: true });
    else
        return $q.reject({ message: 'User has no token' });
};

return authFactory;

})

.factory('AuthToken', function($window) {

var authTokenFactory = {};

//Get auth token
authTokenFactory.getToken = function() {
    return $window.localStorage.getItem('token');
};

//Set or clear auth token
authTokenFactory.setToken = function(token) {
    if (token) {
        $window.localStorage.setItem('token', token);
    };
};

return authTokenFactory;

})

.factory('AuthInterceptor', function($q, $location, Auth) {

    //Add token to all requests.

    var authInterceptorFactory = {};

    authInterceptorFactory.request = function(config) {

        var token = Auth.getToken();

        if (token) {
            config.headers['x-access-token'] = token;
        };

        return config;

    };

    authInterceptorFactory.responseError = function(response) {

        if (response.status == 403) {
            AuthToken.setToken();
            $location.path('/login');
        };

        return $q.reject(response);
    };

    return authInterceptorFactory;

});

mainCtrl.js

 angular.module('mainCtrl', [])

 .controller('mainController', function($rootScope, $location, Auth) {

   var vm = this;

   // get info if a person is logged in
   vm.loggedIn = Auth.isLoggedIn();

   // check to see if a usre is logged in on every request
   $rootScope.$on('$routeChangeStart', function() {
     vm.loggedIn = Auth.isLoggedIn();

     // get user information on route change
     Auth.getUser()
       .success(function(data) {
         vm.user = data;
       });
   });  

   // function to handle login form
   vm.doLogin = function() {

     // call the Auth.login() function
     Auth.login(vm.loginData.username, vm.loginData.password)
       .success(function(data) {

         // if a user successfully logs in, redirect to users page
        if (data.success)
          $location.path('/users');
        else
          vm.error = data.message;

       });
   };

   // function to handle logging out
   vm.doLogout = function() {
     Auth.logout();
     // reset all user info
     vm.user = {};
     $location.path('/login');
   };

 });

1 个答案:

答案 0 :(得分:1)

在您的代码AuthInterceptor中,您需要Auth服务,该服务正在使用$http。我相信你在这里犯了一个错误,就像你使用AuthToken而不是Auth的拦截器一样。

.factory('AuthInterceptor', function($q, $location, AuthToken) { // <--- Here

//Add token to all requests.

var authInterceptorFactory = {};

authInterceptorFactory.request = function(config) {

    var token = AuthToken.getToken();

    if (token) {
        config.headers['x-access-token'] = token;
    };

    return config;

};

authInterceptorFactory.responseError = function(response) {

    if (response.status == 403) {
        AuthToken.setToken();
        $location.path('/login');
    };

    return $q.reject(response);
};

return authInterceptorFactory;

});

编辑:

至于评论中的问题,您尝试使用$q服务而不将其传递到Auth服务

.factory('Auth', function($q, $http, AuthToken) { // <--- was missing here

var authFactory = {};

    ...

authFactory.getUser = function() {

    if (AuthToken.getToken())
        return $http.get('/api/users/me', { cache: true });
    else
        return $q.reject({ message: 'User has no token' }); // <-- you use it here
};

return authFactory;

})