Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angularjs ngDialog.openConfirm dialog appears after whole function execution

In angularjs I want to show confirm dialog on delete operation my code is below :

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
 }
}

But dialog is displayed after whole function execution, so in if condition result variable get undefined value

like image 574
Shb Avatar asked May 11 '15 11:05

Shb


1 Answers

Callback are asynchronous. So, you have to perform your operation in your success callback like this:

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 
                });
    }
like image 76
Pierre-Alexandre Moller Avatar answered Nov 19 '22 06:11

Pierre-Alexandre Moller