angularjs - Accessing ui-bootstrap modal dismiss a

2019-07-21 09:11发布

问题:

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