【发布时间】:2016-06-16 22:05:42
【问题描述】:
我正在使用 AngularJs 和模块 ngSweetAlert http://oitozero.github.io/ngSweetAlert/#/home,我必须在 for 循环中等待确认按钮功能中的指令执行:
for (var i = 0; i < myArray.length; i++) {
SweetAlert.swal({
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
}, function(isConfirm) {
if (isConfirm) {
// Instructions using myArray
} else {
}
});
}
【问题讨论】:
-
@4castle 这与循环中的 JS 闭包无关。它关于使回调的调用阻塞循环的处理。
-
试一试我的解决方案,但真的很快...您是否要创建 array.length 一次出现的确认框数量?因为我很确定那将是你所拥有的结果。每次调用 sweetalert.swal 函数时,它都会创建一个确认框,因此您可能会获得很多确认框,或者您将获得一个将被覆盖的确认框。您可能希望将甜盒函数调用移出循环
-
@AsadSaeeduddin 你是对的,但他们需要使用闭包来正确执行此操作,否则,
i将是myArray.length对于每个回调。 -
他们偶然需要关闭的事实并不会使这与链接的问题重复。这不是“重复关闭”的用途。
标签: javascript angularjs for-loop sweetalert