Sunil Garg Sunil Garg - 14 days ago 5
AngularJS Question

how to control back in the same method from where $modal was opened in angularjs

I have a controller method from where I open $modal popup. Now if I close my popup then I need my control back to the function from where I opened the popup.

Here is my controller

angular.module('TechHealApp').controller('Dashboard', function ($scope,$modal) {

$scope.openPopUp = function (templateUrl, controller, size) {
var modalInstance = $modal.open({
templateUrl: templateUrl,
controller: controller,
size: size,
});
modalInstance.result.then(function (selectedItem) {

}, function () {

});
};

$scope.openIt = function () {
$scope.openPopUp('abc.html', 'controllerA', 'sm');
// I need my control here after closing the popup

};
});


Is there any way to do this ?

Answer

You need to make openPopUp function return promise (modalInstance.result) and use it in openIt function:

$scope.openPopUp = function(templateUrl, controller, size) {
    return $modal.open({
        templateUrl: templateUrl,
        controller: controller,
        size: size,
    }).result;
};

$scope.openIt = function() {
    $scope.openPopUp('abc.html', 'controllerA', 'sm').then(function(data) {
        // popup was closed (ok)
    }, function() {
        // popup was dismissed (cancel)
    });
};

Now, when closing popup from controllerA you can pass additional data and use it in then callbacks (note data parameter):

 // in controllerA when need to close modal
 $modalInstance.close({something: 123});