这可能是我没有正确处理依赖注入的问题,但我正在尝试为我的 SessionsController 提供“会话”服务。这是我的会话控制器:
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 的模块。