打开嵌套模态并关闭当前(父)模态

时间:2019-02-15 18:11:36

标签: angularjs bootstrap-ui

controller1中,我将打开一个这样的模式:

angular.module('myApp').controller('controller1', ['$scope', '$uibModal', function($scope, $uibModal) {

    $scope.openFirstModal = function() {

        var modalScope = $scope.$new();

        var modalInstance = $uibModal.open({
            templateUrl: 'views/modal1.html',
            controller: 'modal1Controller',
            scope: modalScope,
            resolve: {},
            size: 'lg'
        });

        modalScope.uibModalInstance = modalInstance;
    };

}]);

然后,我要打开一个嵌套的模态,如下所示:

angular.module('myApp').controller('modal1', ['$scope', '$uibModal', function($scope, $uibModal) {

    $scope.openSecondModal = function() {

        // Open nested modal
        var modalScope = $scope.$new();

        var modalInstance = $uibModal.open({
            templateUrl: 'views/modal2.html',
            controller: 'modal2Controller',
            scope: modalScope,
            resolve: {},
            size: 'lg'
        });

        modalScope.uibModalInstance = modalInstance;

        // Close this modal
        $scope.uibModalInstance.dismiss('cancel');
    };

}]);

但最后一部分:

        // Close this modal
        $scope.uibModalInstance.dismiss('cancel');

...正在引起嵌套模态中的问题。在我的嵌套模式中没有任何效果:

angular.module('myApp').controller('modal2', ['$scope', '$uibModal', function($scope, $uibModal) {

    $scope.test = function() {
        console.log("test");
    };

}]);

如果我删除有问题的代码段,则嵌套模式可以正常工作。

如何在不使嵌套模态失效的情况下关闭第一个模态?

1 个答案:

答案 0 :(得分:0)

啊,刚发现问题。

我更改了此内容

    // Open nested modal
    var modalScope = $scope.$new();

与此:

    // Open nested modal
    var modalScope = $rootScope.$new();

这样,嵌套模态不依赖于其父代。 父母可以死,孩子可以继续生活