Javascript 在AngularJS中为我的SessionController提供会话服务

Javascript 在AngularJS中为我的SessionController提供会话服务,javascript,angularjs,Javascript,Angularjs,这可能是我没有正确处理依赖注入的问题,但我正在尝试为我的SessionController提供一个“会话”服务。这是我的会话控制器: angular.module('App.controllers').controller('SessionsController', ['$scope', '$location', '$cookieStore', 'Session', function($scope, $location, $cookieStore, Session) { $scope.fo

这可能是我没有正确处理依赖注入的问题,但我正在尝试为我的SessionController提供一个“会话”服务。这是我的会话控制器:

angular.module('App.controllers').controller('SessionsController', ['$scope', '$location', '$cookieStore', 'Session', function($scope, $location, $cookieStore, Session) {

  $scope.foo = function() {
    console.log("clicked foo..");
  }

  $scope.session = Session.userSession;


  $scope.create = function() {

    if ( Session.signedOut ) { 
      $scope.session.$save()
      .success(function(data, status, headers, config) {
        $cookieStore.put('_app_user', data);
      }); 
    }   

  }; 

  $scope.destroy = function() {
    $scope.session.$destroy();
  };  


}]);
以下是实际的会话服务定义:

angular.module('App.services').service('Session',[ '$cookieStore', 'UserSession', 'UserRegistration', function($cookieStore, UserSession, UserRegistration) {
  this.currentUser = $cookieStore.get('_app_user');
  this.signedIn = !!$cookieStore.get('_app_user');
  this.signedOut = !this.signedIn;
  this.userSession = new UserSession( { email:"foo@bar.com", password:"example", remember_me:true } );
  //this.userRegistration = new UserRegistration( { email:"foo-" + Math.floor((Math.random()*10000)+1) + "@bar.com", password:"example", password_confirmation:"example" } );

  $rootScope.$on('$routeChangeStart', function (current, next) {
    if (this.signedOut && next !== '/login') {
     console.log("relocating user..");
        //$location('/login');
    }});

}]);
下面是我如何把它串在一起的:

angular.module('App.resources', ['ngResource']);
angular.module('App.services', ['ngResource']);
angular.module('App.directives', []);
angular.module('App.filters', []);
angular.module('App.controllers', ['ngCookies', 'Session']);

var App = angular.module("App", ['App.resources', 'App.services', 'App.directives', 'App.filters', 'App.controllers', 'ui.compat', '$strap.directives', 'templates']);

显然缺少了一些东西来将所有东西串在一起,因为Firebug抱怨没有名为:Session的模块。

Firebug是对的,您没有名为Session的模块。在App.services模块中有一个名为Session的服务

只需移除会话模块注入,就可以开始了

angular.module('App.controllers', ['ngCookies']);

有趣。现在,当我这样做时,我得到
错误:未知提供者:UserSessionProvider
。不过,我想这是另一个问题。在本例中,这意味着您正在尝试使用UserSession服务,但尚未创建它。我想用户注册也会有同样的体验。我会给你提供一个文档的链接,但现在似乎已经关闭了。但是您需要创建角度服务和注入角度服务(或理解依赖注入)部分。