angularjs - Accessing ui-bootstrap modal dismiss and close function from another controller

断了今生、忘了曾经 提交于 2019-12-11 03:17:18

问题


i have service for modal invoking as shown.

.service('modalService', ['$modal', function ($modal) {

        var modalDefaults = {
            backdrop: 'static',
            keyboard: true,
            modalFade: true
        };

        this.showModal = function (template) {

            modalDefaults.template = template;

            modalDefaults.controller = 'modalController';
            return $modal.open(modalDefaults).result;
        };

    }])

.controller('modalController', function ($scope, $modalInstance) {
        console.log('modalController');
        $scope.ok = function () {
            $modalInstance.close('okResult');
        };
        $scope.cancel = function (result) {
            $modalInstance.dismiss('cancel');
        };
    })

I invoke this modal from a controller say 'controllerA'. The template used for modal contains custom drectives. I need to get the modal controller functionalities like $modalInstance.dismiss and $modalInstance.close in the controller of the custom directive controller of the template, say 'controllerB'.

My template is like this:

<custom-form></custom-form>

回答1:


There are two way to call method of another controller

1 Sharing controller

we can share controller's method using $controller in app.controller

2.Sharing rootScope

share $rootScope both controller using $rootScope



来源:https://stackoverflow.com/questions/30972363/angularjs-accessing-ui-bootstrap-modal-dismiss-and-close-function-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!