javascript - 仅针对一个 Controller 未定义 Angular 服务字段

标签 javascript angularjs dependency-injection controllers angular-services

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

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

我用它来设置和共享 Controller 之间的身份验证状态。在我的 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("/");
        }
      };

    }]);

但是无法从此 Controller 访问它,即使我很确定我正确注入(inject)了服务:

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

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

引用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.

关于javascript - 仅针对一个 Controller 未定义 Angular 服务字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948732/

相关文章:

typescript - 我可以直接访问 DI 注入(inject)器以使用 NestJS 实例化类吗

c# - 是否可以在创建 Autofac LifetimeScope 后向其添加注册?

javascript - tinymce 4.3.4 video_template_callback

Javascript - 赋值更新父元素?

javascript - 当输入值更改时,将多个输入字符串推送到范围数组 (AngularJS)

angularjs - Angular 数据表隐藏列

javascript - 如何在渲染函数中使用 if 进行比较

javascript - 如何在 javascript 中设置 svg 子属性的颜色?

Angularjs - IE9 缓存行为

c# - 如何让ServiceProvider对手动创建的对象调用Dispose?