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

Open a nested modal and close current (parent) modal

来自 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");
    };

}]);

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

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

啊,刚发现问题

我改变了这个:

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

有了这个:

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

这样嵌套模式就不会依赖他的 parent。 parent可以死而child会继续活着