简体   繁体   English

如何从外部控制器关闭Angular Modal

[英]How to close an Angular Modal from the outside controller

I am using angular-modal-service to open a modal. 我正在使用angular-modal-service打开模态。

$scope.showLoader = function(message) {
    ModalService.showModal({
        templateUrl: "/templates/loader.html",
        controller: "ctrl",
        inputs: {
            message: "loading"
        }
    }).then(function(modal) {
        modal.close.then(function(result) {
            if (result) {
                // do something
            }
        });
    });
}

After I open this modal, I want to call a function to close it from the main controller 打开此模式后,我想从主控制器调用函数以将其关闭

$scope.closeLoader = function() {
    // close the modal here
}

How can I do this? 我怎样才能做到这一点?

You can assign the modal closing function to the $scope when the modal is completely loaded. 模态完全加载后,可以将模态关闭函数分配给$scope I added a dummy function, that it won't execute undefined if the modal is closed, before it is ready. 我添加了一个伪函数,如果模态关闭,它不会在undefined准备好之前执行undefined函数。 You can also skip that, if you can assure, that it won't be possible. 如果可以保证,也可以跳过该操作。

$scope.closeLoader = angular.noop;

$scope.showLoader = function(message) {
    ModalService.showModal({
        templateUrl: "/templates/loader.html",
        controller: "ctrl",
        inputs: {
            message: "loading"
        }
    }).then(function(modal) {
        $scope.closeLoader = modal.close;
    });
};

Below is the code: 下面是代码:

app.controller('MainController', function($scope, close) {

 $scope.close = function(result) {
    close(result, 500); // close, but give 500ms for bootstrap to animate
 };

});

html html

<button type="button" ng-click="close('No')" class="btn btn-default" data-dismiss="modal">No</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM