我有2项服务。一个服务(TimeEventService)将数据保存到数据库表。另一个服务(ReportingService)是TimeEventService的包装器,并为控制器提供功能。
我需要ReportingService等待已保存的'来自TimeEventService的响应。请问最好和最简单的方法是什么。
TimeEventService代码:
angular.module('TimeEventServiceMod', ['ngResource']).
factory('TimeEventService', function(TestTimeEventService,$filter) {
var timeEventData = {
savedEvent:null
};
timeEventData.newEvent = function(ataskSequenceId,eventNumber){
var newEvent = new TestTimeEventService();
newEvent.tasksequenceid = ataskSequenceId;
newEvent.event = eventNumber;
newEvent.timeofevent = new Date();
newEvent.$save(function(){
timeEventData.newEvent=newEvent;
console.log('saved event OK: ' + angular.toJson(timeEventData.newEvent));
return timeEventData;
},function (errorResponse) {
// failure callback
console.log('error saving Event and new status to db:' + angular.toJson(errorResponse));
});
};

ReportingService代码:
angular.module('ReportingServiceMod', ['ngResource','TimeEventServiceMod']).
factory('ReportingService', function(TimeEventService) {
var reportData = {
lastTimeEventId:-1
};
var timeEventData = TimeEventService;
//the public test events
reportData.newEvent_testStart = function(ataskSequenceId){newEvent(ataskSequenceId,10)};
newEvent = function(ataskSequenceId, eventid){
timeEventData = TimeEventService.newEvent(ataskSequenceId,eventid);
if (timeEventData.savedEvent!=null)
reportData.lastTimeEventId = timeEventData.savedEvent.id;
console.log('saved time event id:' + reportData.lastTimeEventId);
};
return reportData;
})

注意该行(if(timeEventData.savedEvent!= null))不起作用,因为该对象尚未保存。
答案 0 :(得分:1)
angular
.module('myApp',[])
.run(function($rootScope){
$rootScope.title = 'myTest Page';
})
.controller('testController', ['$scope','testFactory1','testFactory2', function($scope, testFactory1, testFactory2){
testFactory1.firstCall()
.then(function(reply){
if(reply){
testFactory2.secondCall()
.then(function(reply){
if(reply){
}
},function(err){
console.log("there was a error", err);
});
}
},function(err){
console.log("there was a error", err);
});
}]).factory('testFactory1',['$http','$q', function($http, $q){
var ser1 = {
firstCall : firstCall,
}
return ser1;
function firstCall(){
var first = $q.defer();
$http.get(....)
.then(function(result){
first.resolve(result.data);
},function(error){
first.reject(error.data);
})
return first.promise;
}
}]).factory('testFactory2',['$http','$q', function(){
var ser2 = {
secondCall : secondCall,
}
return ser2;
function secondCall(){
var second = $q.defer();
$http.get(....)
.then(function(result){
second.resolve(result.data);
},function(error){
second.reject(error.data);
})
return second.promise;
}
}]);
说明:
Why dont you call services from controllers.
here we have two facotries `testFactory1` and `testFactory2` and each have a http call.
So you can call these from controllers one after another.
in 'testController' you are calling 'firstCall' http function first, and on its sucess we are making 'secondCall',
We can use $q service to wait for secondCall to make till firstCall finishes.