如何使用$q从AngularJS服务返回一个已解决的Promise?
如何使用$q从AngularJS服务返回一个已解决的Promise?
我的服务是:
myApp.service('userService', [ '$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) { var deferred; deferred = $q.defer(); this.initialized = deferred.promise; this.user = { access: false }; this.isAuthenticated = function() { this.user = { first_name: 'First', last_name: 'Last', email: '[email protected]', access: 'institution' }; return deferred.resolve(); }; } ]);
我在我的`config`文件中调用它:
myApp.run([ '$rootScope', 'userService', function($rootScope, userService) { return userService.isAuthenticated().then(function(response) { if (response.data.user) { return $rootScope.$broadcast('login', response.data); } else { return userService.logout(); } }); } ]);
然而,它投诉`then`不是一个函数。我难道没有返回已解析的promise吗?