【发布时间】:2016-09-06 03:54:45
【问题描述】:
我正在循环遍历一个数组,并且在每个循环中,我添加一个 promise 数组,然后将其传递给 $q.all。每个链包括一个confirm() 对话和一个modal。用户的事件顺序应该是确认() - 模式 - 确认() - 模式。相反,我得到了确认() - 确认() - 模态 - 模态。另外,我希望在最后一个模式关闭后执行一个函数 refreshSelection(),但它目前会在最后一个 confirm() 对话关闭后立即触发。
var promiseArr = [];
var selection = []; // [obj1, obj1, obj3...]
for (var i = 0; i < selection.length; i++) {
promiseArr.push(getData(i));
}
$q.all(promiseArr)
.then(refreshSelection);
function getData(i) {
var opts = {}; // selection[i].id is param
return $http(opts)
.then(onGetSuccess(i));
}
function onGetSuccess(i) {
return function(result) {
var shouldOpenModal = confirm(selection[i].text);
if (shouldOpenModal == true) {
openModal();
}
}
}
function openModal() {
var copyPunchesModal = $uibModal.open({
// modal template options
}
});
copyPunchesModal.result.then(otherFunc, function () {
console.log("Modal closed");
});
}
function refreshSelection() {
selection = [];
}
我也尝试了以下方法,但无济于事。
//...
function getData(i) {
var opts = {}; // selection[i].id is param
return $http(opts)
.then(onGetSuccess(i))
.then(openConfirm)
.then(openModal);
}
非常感谢您的帮助。
【问题讨论】:
标签: javascript angularjs