【问题标题】:call a function from ui bootstrap modal controller从 ui bootstrap modal 控制器调用函数
【发布时间】:2019-03-04 09:30:56
【问题描述】:

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

我将这个函数定义为从 $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();
        }


    });

【问题讨论】:

    标签: javascript angularjs angular-ui-bootstrap


    【解决方案1】:

    您可以使用 modalInstance 回调,它在 modal 关闭时调用

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

    【讨论】:

    • 谢谢,我知道了,但是如果这个函数还在根作用域里会有问题吗?
    • 尽量避免rootscope
    • 非常感谢,那么不建议使用rootscope。
    • $rootscope 是一个非常有用的功能,只是不适用于这个特定的用例
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多