$ q.allPromises返回一个数组,但我只想要一个元素,而不是全部

我在用着

$q.allPromises(http request).then (function (data) { //more code logic }) 

它第一次工作,但我24小时后调用这个方法,发现“数据”是一个对象的数组,每当我调用$ q.allPromises与新的http json对象时,它被追加。

我怎么能忘记数组中的旧“对象”。 我每24小时拉一个json,只关心我刚取下的json对象。 我想忽略从前面的http promise请求中拉下来的json对象,但它似乎继续追加到一个数组

我尝试添加

 $q.allPromises(http request).then (function (data) { //more code logic data.shift (); }) 

shift()应该是从数组中删除第一个元素,但它似乎不工作。

你不需要使用$q.all$http提供者本身返回一个promise:

 $http.get({...}).then(function(response) { console.log(response.data) // this will print actual data }); $http.post({...}).then(function(response) { console.log(response.data) // this will print actual data }); 

$q.all是一个特殊的方法,用于在采取行动之前等待许多承诺解决,如下所示:

 var promiseA = $http.get({...}).then(function(response) { console.log(response.data) // this will print actual data }); var promiseB = $http.post({...}).then(function(response) { console.log(response.data) // this will print actual data }); var arrayOfPromises = $q.all([promiseA, promiseB]).then(function(arrayOfResults) { console.log(arrayOfResults); // this will print an array of the results of the http requests });