我想把一些数据放在异步调用返回的作用域上。
我在工厂里有一个名为公司的数组。AngularJS等待单个异步调用
factory.getByCategoryId = function (id) {
$http.get('http://localhost/campaign?access-token=12345&id=2').then(
function (result) {
factory.companies = [];
if (result.data.length !== 0) {
for (var index = 0; index < result.data.length; index++) {
factory.companies.push(result.data[index]);
}
} else {
console.log('Result is not set.', result);
}
},
function (result) {
console.log('Failed', result);
}
);
}
调用函数:
$scope.closeModal = function (index) {
if (index) {
var promises = []
promises.push(Service.getByCategoryId($scope.categories[index].id));
$q.all(promises).then(function() {
$scope.myItems = Service.all(); //return all from factory.companies
$scope.refreshItems(); //this function refreshes the DOM using myItems
});
}
}
调用住就是同这个功能首次factory.companies但之后的第二调用它的更新。我敢肯定,应用程序应该等待更多,但不知道如何。
KöszönömAlex,sokatsegítettél! (: – MagicDragon