In angularjs dialog ngDialog.openConfirm appears after whole function is executed
In angularjs i want to show confirmation 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 ' +
'<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 after executing the whole function, a dialog is displayed, so in case the condition result variable gets undefined
+3
source to share
1 answer
The callback is asynchronous. So, you should 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 ' +
'<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
});
}
+9
source to share