我有以下控制器代码:
.controller('Controller1',function ($scope,MyService) { var promise = MyService.getData(); promise.then(function(success) { console.log("success"); },function(error) { console.log("error"); },function(update) { console.log("got an update!"); }) ;
}
在我的services.js中:
.factory('MyService',function ($resource,API_END_POINT,localStorageService,$q) { return { getData: function() { var resource = $resource(API_END_POINT + '/data',{ query: { method: 'GET',isArray: true } }); var deferred = $q.defer(); var response = localStorageService.get("data"); console.log("from local storage: "+JSON.stringify(response)); deferred.notify(response); resource.query(function (success) { console.log("success querying RESTful resource") localStorageService.add("data",success); deferred.resolve(success); },function(error) { console.log("error occurred"); deferred.reject(response); }); return deferred.promise; } } })
但是由于某种原因,deferred.notify调用似乎没有执行并在控制器中被接收.这里有没有什么不对?我不知道如何使通知执行.