Angular 仅对一个控制器未定义服务字段

Angular service field is undefined for one controller only

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

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。

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) {

换句话说,您需要确保函数中参数的顺序与前面的参数列表相匹配 names/dependencies。

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

Inline Array Annotation

This is the preferred way to annotate application components. This is how the examples in the documentation are written.

For example:

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

Here we pass an array whose elements consist of a list of strings (the names of the dependencies) followed by the function itself.

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration.

你的注射顺序有误 应该是

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

服务名和传入的服务名要顺序一致