从子模式控制器调用父控制器方法 (ui bootstrap)

Call a parent controller method from child modal controller (ui bootstrap)

所以这是我遇到的问题,就像我有父控制器和子控制器,它是模态控制器,我在父控制器中有一个方法,我想从子模态控制器调用,我不知道知道我错过了什么,但这就是我尝试过的。

  App.controller('MailFolderController', ['$scope', '$http', '$timeout', '$stateParams', '$window', 'mails', '$interval', function ($scope, $http, $timeout, $stateParams, $window, mails, $interval) {


$scope.check = function(){
    console.log("call parent ==========>")
}


  App.controller('orderCancellationController', ['$scope', '$modal', function ($scope, $modal) {

    $scope.open = function (mail) {
        var modalInstance = $modal.open({
            templateUrl: '/orderCancellationBox.html',
            controller: ModalInstanceCtrl,
            resolve: {
                mail: function () {
                    return mail;
                }
            }
        });
    };

    // Please note that $modalInstance represents a modal window (instance) dependency.
    // It is not the same as the $modal service used above.

    var ModalInstanceCtrl = function ($scope, $modalInstance, mail) {

        $scope.mail = mail;
        $scope.submit = function () {
            $scope.$parent.check();
            $modalInstance.close('closed');
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    };
    ModalInstanceCtrl.$inject = ["$scope", "$modalInstance", 'mail'];

}]);


}]);

但它给我一个错误,没有这样的功能,我在检查方法上遇到错误,我想从模态实例控制器调用这个检查方法,但无法执行,请帮助。

https://angular-ui.github.io/bootstrap/#/modal

bootstrap 中的模式有一个 'scope' 选项,

scope - a scope instance to be used for the modal's content (actually the $modal service is going to create a child scope of a provided scope). Defaults to $rootScope

使用 scope: $scope 应该允许您使用父控制器中定义的方法

示例:

    $scope.open = function (mail) {
    var modalInstance = $modal.open({
        templateUrl: '/orderCancellationBox.html',
        controller: ModalInstanceCtrl,
        scope: $scope,
        resolve: {
            mail: function () {
                return mail;
            }
        }
    });
};

在 $modal 中解析 $scope 并简单地调用 $scope.parent.check ,你就完成了!!