Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: function call when closing modal

I imagine this is quite a simple answer, but I can't find the right syntax.

I have my modal opening like so,

$scope.assignment = function (groupId) {
    var modalInstance = $modal.open({
        templateUrl: 'assignment_form',
        controller: 'GroupsAssignmentController',
        windowClass: 'modal-user-window',
        resolve: {
            id: function () {
                return groupId;
            }
        }
    });

All I want to do is have a function run when the modal is closed so my main screen is updated.

I'm not sure if this involves $modal.close?

    $modal.close({
    //getAllGroups();
    });
like image 307
user3407039 Avatar asked Jun 18 '15 10:06

user3407039


1 Answers

modalInstance.result.finally(function(){ 
    // do your work here
});

You can also use then

then(successCallback, errorCallback, notifyCallback) 

SuccessCallback is excetuted when the promise is resolved. errorCallback is executed when the promise is rejected. Finally notifyCallback is executed when notified.

In the case of angular-ui's modal, clicking on the backdrop will result in a rejected promise. With this in mind, your code could be changed to :

modalInstance.result.then(function () {
  alert('Modal success');
}, function () {
  alert('Modal dismissed');
});
like image 59
Nikhil Aggarwal Avatar answered Oct 24 '22 08:10

Nikhil Aggarwal