在angularjs ngDialog.openConfirm对话框出现在整个函数执行后

时间:2015-05-11 11:45:50

标签: javascript angularjs

在angularjs中,我想在删除操作上显示确认对话框,我的代码如下:

function deleteOperation(){
    var result;        
             ngDialog.openConfirm({
                    template:
                        '<p>Are you sure you want to delete selected conversation(s) ?</p>' +
                        '<div>' +
                          '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No&nbsp;' +
                          '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
                        '</button></div>',
                    plain: true,
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    result=true;
                }, function (value) {
                    result=false;
                });

            if (result == true) {
                // perform delete operation
 }
}

但是在整个函数执行后会显示对话框,因此如果条件结果变量得到未定义的值

1 个答案:

答案 0 :(得分:9)

回调是异步的。因此,您必须在成功回调中执行操作,如下所示:

function deleteOperation(){     
             ngDialog.openConfirm({
                    template:
                        '<p>Are you sure you want to delete selected conversation(s) ?</p>' +
                        '<div>' +
                          '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No&nbsp;' +
                          '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
                        '</button></div>',
                    plain: true,
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    // perform delete operation
                }, function (value) {
                    //Do something 
                });
    }