user3407039
user3407039

Reputation: 1335

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

Upvotes: 6

Views: 14522

Answers (1)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28445

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

Upvotes: 10

Related Questions