仅为一个控制器未定义角度服务字段

时间:2015-06-19 23:33:54

标签: javascript angularjs dependency-injection controllers angular-services

我定义了一个简单的服务:

app.service('AuthenticationService', function() {
  var auth = {
    isLogged: false
  };
  return auth;
});

我用它来设置和共享控制器之间的身份验证状态。它在我的LoginCtrl中读得很好:

app.controller('LoginCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService', 
  function LoginCtrl($scope, $location, $window, UserService, AuthenticationService) {

    $scope.login = function logIn(username, password) {
      if (username !== undefined && password !== undefined) {
        UserService.login(username, password)
        .success(function(data) {
          AuthenticationService.isLogged = true; //sets value correctly
          $window.sessionStorage.token = data.token;
          $location.path("/main");
        })
        .error(function(status, data) {
          console.log(status);
          console.log(data);
        });
      }
    };

    //...

}]);

与MainCtrl一样:

    app.controller('MainCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService', 
      function MainCtrl($scope, $location, $window, UserService, AuthenticationService) {

      //...

      $scope.logout = function() {
        console.log("LOGOUT CALLED AT MAIN CONTROLLER. isLogged "
 + AuthenticationService.isLogged); //prints true
        if (AuthenticationService.isLogged) {
          AuthenticationService.isLogged = false;
          delete $window.sessionStorage.token;
          $location.path("/");
        }
      };

    }]);

然而,即使我非常确定我正确地注入了这项服务,它也无法从该控制器进入:

    app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
      function SearchCtrl($scope, $location, $window, UserService, AuthenticationService, MovieDataService) {

    //...

      $scope.logout = function() {
        console.log("LOGOUT CALLED AT SEARCH CONTROLLER. isLogged: "
 + AuthenticationService.isLogged); //prints undefined
        if (AuthenticationService.isLogged) {
          //UserService.logout();
          AuthenticationService.isLogged = false;
          delete $window.sessionStorage.token;
          $location.path("/");
        }
      };
    //...
    }]);

为什么会这样?我没有在任何地方取消isLogged。

2 个答案:

答案 0 :(得分:3)

app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
  function SearchCtrl($scope, $location, $window, UserService, AuthenticationService, MovieDataService) {

应该是

app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
  function SearchCtrl($scope, $location, $window, MovieDataService, UserService, AuthenticationService) {

换句话说,您需要确保函数中参数的顺序与前面的参数名称/依赖项列表相匹配。

引用AngularJS DI documentation(强调我的):

  

内联数组注释

     

这是注释应用程序组件的首选方法。这是   如何编写文档中的示例。

     

例如:

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);
     

这里我们传递一个数组,其元素由一个字符串列表组成(   依赖项的名称)后跟函数本身。

     

使用此类注释时,请注意保留注释   数组与函数声明中的参数同步

答案 1 :(得分:0)

你的注射顺序错了 应该是

['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
      function SearchCtrl($scope, $location, $window, MovieDataService, UserService, AuthenticationService){}]

传入的服务和服务的名称应该是相同的顺序