angularJS中的API调用彼此正确

问题描述:

我在Ionic移动项目中使用角度时遇到了问题,我的这段代码在我的桌面环境中工作得很好。我以500ms的间隔对另一个Web服务器进行三次API调用。但在使用较低连接的电话中,有时会有一个呼叫失败,因此整个流程都会失败。 在上一次完成的时刻,是否有某种方法可以进行API调用?我的意思是,没有使用固定的时间。angularJS中的API调用彼此正确

//get member information 
$timeout(function() { 
    membersService.getMember(community.url, community.user.api_key, community.user.auth_token, $stateParams.userId). 
    success(function(data) { 
    $scope.member = data.result; 
    }); 
}, 500); 

$timeout(function() { 
    messagesService.getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "from"). 
    success(function(data) { 
    if(data.result["entities"].length > 0) { 
     messages = messages.concat(data.result["entities"]); 
     subject = "Re: "+data.result["entities"][data.result["entities"].length-1].title; 
    } 
    $scope.messageData.subject = subject; 
    }); 
}, 1000); 

$timeout(function() { 
    messagesService.getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "to"). 
    success(function(data) { 
    log = []; 
    if(data.result["entities"].length > 0) { 
     angular.forEach(data.result["entities"], function(v, k) { 
     v.owner_guid = $stateParams.userId; 
     log.push(v); 
     }, log); 
     messages = messages.concat(log); 
    } 
    var log = []; 
    var count = 0; 
    angular.forEach(messages, function(v, k) { 
     messages_reorder.push(v); 
    }, log); 
    }); 
}, 1500); 

这是实现嵌套承诺链的结果:

var loadMemberInfo = function(userId) 
{ 
    return membersService 
    .getMember(community.url, community.user.api_key, community.user.auth_token, $stateParams.userId) 
    .then(function(data) 
    { 
     $scope.member = data.result; 
    }); 
}, 
getConversationFrom = function() { 
    return messagesService 
    .getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "from") 
    .then(function(cf) { 
    if(cf.data.result["entities"].length > 0) { 
     messages = messages.concat(cf.data.result["entities"]); 
     subject = "Re: "+cf.data.result["entities"][cf.data.result["entities"].length-1].title; 
    } 
    $scope.messageData.subject = subject; 
    }); 
}, 
getConversationTo = function() { 
    messagesService 
    .getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "to") 
    .then(function(ct) { 
    log = []; 
    if(ct.data.result["entities"].length > 0) { 
     angular.forEach(ct.data.result["entities"], function(v, k) { 
     v.owner_guid = $stateParams.userId; 
     log.push(v); 
     }, log); 
     messages = messages.concat(log); 
    } 
    //order array 
    messages = messages.sort(function(a,b) { return a.time_created - b.time_created }); 

    var log = []; 
    var count = 0; 

    angular.forEach(messages, function(v, k) { 
     messages_reorder.push(v); 
    }, log); 
    }); 
}, 
orderFullConversation = function() { 
    $ionicLoading.hide(); 
    console.log(messages); 
    if(messages_reorder.length > 5) { 
    var messages_partial = messages_reorder.slice(messages_reorder.length-5,messages_reorder.length); 
    } 
    else { 
    var messages_partial = messages_reorder; 
    } 
    $scope.messages = messages_partial; 
    $scope.community = community; 
}; 


loadMemberInfo($stateParams.userId) 
    .then(getConversationFrom) 
    .then(getConversationTo) 
    .then(orderFullConversation); 

更多细节here

您可以使用角度$ q service

你应该看看Angular Promise API。它是为了相同的目的。它可以让你链AJAX的呼声此起彼伏..

// this 
    $http.get('/api/v1/movies/avengers') 
     .success(function(data, status, headers, config) { 
     $scope.movieContent = data; 
     }); 

    // is the same as 
    var promise = $http.get('/api/v1/movies/avengers'); 

    promise.then(
     function(payload) { 
     $scope.movieContent = payload.data; 
     }); 

更多详细信息here