5

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

2
  • You should execute the part perform delete operation inside the then callback where you set result=true. You could also call a function from there that performs the delete. This is the right place to do it
    – devnull69
    May 11, 2015 at 11:49
  • yes, you are right performing delete operation inside then callback works, thanks
    – Shoaib
    May 11, 2015 at 11:58

1 Answer 1

10

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 
                });
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.