【问题标题】:Pause execution while AngularUI modal is openAngularUI 模式打开时暂停执行
【发布时间】:2015-06-27 21:08:35
【问题描述】:

我正在循环浏览 AngularJS 中的一些项目,并使用 AngularUI 模式询问用户对每个项目的输入。我的问题是循环结束并且所有模态都立即呈现,而无需等待用户。

如何让执行等到模态关闭?

我的代码示例:

var listofitems = somelist;
// loop through each item
for (var i in listofitems){
    if (listofitems[i].id == presetId){
        // ask user confirmation in a modal
        $scope.selections = {'doThis': doThis,
                             'doThat': doThat}
        var openModal = function () {
            var modalInstance = $modal.open({
                 templateUrl: 'confirmationModal.html',
                 controller: confirmationController,
                 resolve: {
                     selections: function () {
                          return $scope.selections;
                     }
                 }
            });

            modalInstance.result.then(function (selections) {
                doThis = selections.doThis;
                if (selections.doThat){
                   doThis = selections.doThis;
                }
          });
        }
        // open the modal
        openModal();                                                
      }
   }
}


var confirmationController = function ($scope, $modalInstance, selections) {

    $scope.selections = selections;

    $scope.doThis = function () {
        $scope.selections.doThis = true;
        $modalInstance.close($scope.selections);
    };

    $scope.doThat = function () {
        $scope.selections.doThat = true;
        $modalInstance.close($scope.selections);
    };
};

在此处合并@dsfg 答案是Plunkr example。 ui modal 不能很好地工作,但是您可以看到在用户提交任何输入之前执行已经完成。

【问题讨论】:

    标签: angularjs angular-ui angular-ui-bootstrap


    【解决方案1】:

    你不能只是暂停循环(你可以使用 ES6 生成器)。但是您可以更改遍历数组/对象键的方式。您可以做的是使用函数逐个检查项目,只有在上一个完成时才执行下一个“迭代”。使用 Promise 很容易。

    首先,让openModal 返回承诺,然后创建辅助函数checkItems,它将为键数组中的每个项目调用。

    var openModal = function() {
        var modalInstance = $modal.open({
            templateUrl: 'confirmationModal.html',
            controller: confirmationController,
            resolve: {
                selections: function() {
                    return $scope.selections;
                }
            }
        });
    
        return modalInstance.result.then(function(selections) {
            doThis = selections.doThis;
            if (selections.doThat) {
                doThis = selections.doThis;
            }
        });
    };
    
    var listofitems = somelist;
    var keys = Object.keys(listofitems);
    
    (function checkItems() {
    
        // get the first key and remove it from array
        var key = keys.shift();
    
        if (listofitems[key].id == presetId) {
            // ask user confirmation in a modal
            $scope.selections = {
                'doThis': doThis,
                'doThat': doThat
            }
            // open the modal
            openModal().then(function() {
                if (keys.length) {
                    checkItems();
                }
            });
        }    
    })();
    

    在上面的例子中,项目将被检查直到第一个承诺被拒绝。如果您想检查所有项目而不考虑承诺状态,请使用

    openModal().finally(function() {
        if (keys.length) {
            checkItems();
        }
    });
    

    【讨论】:

    • 这真的让我很困惑。我确定我正确地遵循了您的示例,并且看起来模态是连续打开的,而不是同时打开的。但是执行仍然像往常一样继续。记录显示then() 没有被执行,但不知何故checkItems() 被调用,直到没有更多的键。我去看看能不能搞定。
    • 我在我这边编辑了代码,所以即使 id 不匹配也会调用if( keys.length ){ checkItems();}
    • 好吧,您将代码放入不会暂停的循环中。而是尝试将您的代码重构为一组承诺,并在所有承诺都解决后设置完成标志。 plnkr.co/edit/1Eq5kNLOun9kn6PXsXhT?p=preview
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多