从ui引导模态控制器调用函数

时间:2019-03-04 09:30:57

标签: javascript angularjs angular-ui-bootstrap

我有一个使用自己的新控制器打开的模态,我想从模态控制器调用一个函数,但是该函数是在父控制器中定义的。

我定义了要从$ rootScope调用的函数,这是在模式中从父级调用函数的最佳方法,还是将来有意义?

示例:

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


    });

1 个答案:

答案 0 :(得分:1)

  

您可以使用modalInstance回调,该回调在模式已关闭时调用

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

模式控制器

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

            //save Event
            $scope.save = function () {
             $uibModalInstance.close();
            }
    })