-1

このエラーが発生しました「エラー: [$injector:cdep] 循環依存関係が見つかりました: $cookies <- $cookies <- AuthService」

次のコードで

'use strict';

angular.
  module('core.auth').
  factory('AuthService', [ '$http', '$rootScope', 'ConfigService', '$cookies',
    function AuthService($http, $rootScope, ConfigService, $cookies) {
      const service = {};

      service.Login = Login;
      service.SetCredentials = SetCredentials;
      service.ClearCredentials = ClearCredentials;

      return service;

      function Login(username, password, callback) {
        $http.post(ConfigService.LOGIN_API, { username: username, password: password })
           .success(function (response) {
               callback(response);
           });
      }

      function SetCredentials(username) {
        $rootScope.globals = {
          currentUser: {
            username: username
          }
        };

        $cookies.put('globals', $rootScope.globals);
      }

      function ClearCredentials() {
        $rootScope.globals = {};
        $cookies.remove('globals');
      }
    }
]);

そして、私はログインコンポーネントでこのサービスを使用しています

'use strict';

angular.
  module('login').
  component('login', {
    templateUrl: 'dist/components/login/login.template.html',
    controller: ['$location', 'AuthService', '$log',
      function LoginController($location, AuthService, $log) {

      (function initController() {
          // reset login status
          AuthService.ClearCredentials();
        }());

        this.login = () => {
          AuthService.Login(this.username, this.password, (response) => {
            if (response.success) {
              $log.log(response);
              $log.log('Login successful', true);
              AuthService.SetCredentials(this.username);
              $location.path('#!/products');
            } else {
              $log.log(response)
            }
          })
        }

      }
    ],

    controllerAs: 'vm'
});

私のコードの何が問題なのか理解できません...ここから $cookies を削除すると、完全に機能します。おそらく解決策は、独自のCookieサービスを作成することですか?:)

4

1 に答える 1