首页 > 解决方案 > $mdDialog.show(confirm) 的问题,无法在 AngularJS 中执行此对话框指令下方的代码

问题描述

我发现这个问题$mdDialog.show(confirm),对话框函数下面的代码甚至在弹出窗口出现之前就执行了。

我可以通过设置一个标志来限制下面代码的执行。但是在对话活动完成后,它会在Error(ie, resolvePromise())不执行以下代码的情况下进入:

//This is the code I have used.

var confirm = $mdDialog.confirm()
             .title('Early Dispatch Date Available.')
             .textContent('Do you want to reschedule and Change Quantity.')
              .ariaLabel('day')
             .ok('Yes')
             .cancel('Plan on Date');

$mdDialog.show(confirm).then(function (obj)
{
    $scope.targetDates[lastItem].Qty = 0;
    return;
}, function () {                
    condition = 'QtyLessThanCap';
});

//for example,this is the code which gets executed even before the comfirmation dialog pops up..

angular.forEach($scope.targetDates, function (Datess) 
{
      totalCalQty = totalCalQty + parseInt(Datess['Qty']);                   
});

我希望代码按照我编码的顺序执行,我的意思是下面的代码只需要在确认对话框活动之后执行。

先感谢您。

标签: c#angularjsangularjs-directiveangular-materialmddialog

解决方案


我能够处理相同类型的功能的唯一方法是拆分函数,例如,您有一个动作函数来确定您是需要模态还是可以计算总数:

function something() { // if modal show modal.then(doSomething) }

然后是可以直接调用的实际计算函数,也可以从任何级别的 mdModal.then() 调用:

function doSomething() { // calculate total }

它可能不像你想要的那么漂亮,但它确实有效。


推荐阅读