0

I have a modal that's opened with a new controller of its own, and I want to call a function from the modal controller, but the function is defined in the parent controller.

I defined this function to be invoked from $rootScope, is this the best way to call a function from a parent in the modal or will it make sense in the future?

Example:

  FormModule.controller("formCtrl", function ($scope, $http, $uibModal, $log, $rootScope) {

        $rootScope.ShowReport = function ShowReport() {

        //function Edit
        $scope.edit = function () {
            var ObjResolve = function () {
                return obj;
            }
            var modalInstance=  $uibModal.open({
                animation: true,
                templateUrl: 'Modal.html',
                controller: 'ModalInstanceCtrl',
                resolve: {
                    ObjResolve
                }
            }).result.catch(function (res) {
                if (!(res === 'cancel' || res === 'escape key press')) {
                    //throw res;
                }
            });
        };

    });
 FormModule.controller("ModalInstanceCtrl", function ($scope, $uibModal, $uibModalInstance, $http, ObjResolve, $rootScope ) {

        //save Event
        $scope.save = function () {
         $rootScope.ShowReport();
        }


    });
Gargaroz
  • 313
  • 9
  • 28
Rawan Mansour
  • 111
  • 2
  • 16

1 Answers1

1

You can use modalInstance callback which is called when modal is CLOSED

$scope.edit = function () {
 var ObjResolve = function () {
   return obj;
 }
 var modalInstance=  $uibModal.open({
        animation: true,
        templateUrl: 'Modal.html',
        controller: 'ModalInstanceCtrl',
        resolve: {
        ObjResolve
        }
    }).result.catch(function (res) {
     if (!(res === 'cancel' || res === 'escape key press')) {//throw res;}
    });
    modalInstance.result.then(function(){
        ShowReport() //call function when modal is closed
    })
 };

modal controller

FormModule.controller("ModalInstanceCtrl", function ($scope, $uibModal, $uibModalInstance, $http, ObjResolve, $rootScope ) {

            //save Event
            $scope.save = function () {
             $uibModalInstance.close();
            }
    })
Hussain
  • 87
  • 5