【发布时间】:2016-06-16 12:25:14
【问题描述】:
我正在尝试将一些数据放在异步调用返回的范围内。
我在工厂里有一个数组叫做公司。
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 保持不变,但在第二次调用后它更新了。我确定该应用程序应该以某种方式等待更多,但不知道如何。
【问题讨论】:
标签: javascript angularjs asynchronous ionic-framework q