Angular-UI-Bootstrap Modal不会覆盖现有的模态

时间:2014-04-08 07:05:15

标签: javascript angularjs angular-ui-bootstrap

我已经在我的AngularJS应用的$rootScope上创建了这个alertModal函数。它工作得很好,但是,我需要每个模态来覆盖后续的模态。换句话说,如果已经有模态打开,我希望任何后续模态关闭现有模态。我无法弄清楚如何执行此操作,因为每个$modal实例都会在$rootScope上创建子范围。 $modal中有一个选项可以使用scope来使用(而不是$rootScope),但我不确定如何纯粹为此目的实例化自定义范围。任何帮助或线索都会很棒。

$rootScope.modalAlert = function (alert_type, message, acknowledge) {
    return $modal.open({
        templateUrl: '/partials/modals/alert.html',
        controller: AlertModalInstanceCtrl,
        resolve: {
            data: function () {
                return {
                    type: alert_type,
                    message: message,
                    acknowledge: acknowledge || false
                }
            }
        }
    })
}

var AlertModalInstanceCtrl = function ($scope, $modalInstance, data) {

    var heading = ''

    switch (data.type) {
        case 'error':
            heading = 'Oops'
            break
        case 'working':
            heading = 'Working'
            break
    }

    $scope.alert = {
        type: data.type,
        heading: heading,
        message: data.message,
        acknowledge: data.acknowledge
    }

    $scope.cancel = function () {
        $modalInstance.dismiss('cancelled');
    };
}

不太相关,但这是随附的HTML模板:

<div class="row">
    <div class="col-sm-12">
        <h2 class="modal-header">{{ alert.heading }}</h2>

        <div class="modal-body">
            <div style="text-align: center;">
                <i ng-class="{
                'fa fa-exclamation-triangle fa-5x danger': alert.type=='error',
                'fa fa-info fa-5x info': alert.type=='working'
                }"></i>

                <p>{{ alert.message }}</p>
            </div>
            <button ng-show="alert.acknowledge" class="btn btn-default pull-right" ng-click="cancel()">Ok</button>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

看起来是$broadcast的一个很好的用例:

$rootScope.modalAlert = function (alert_type, message, acknowledge) {
     $rootScope.$broadcast('modal:start-open');
     return $modal.open({ 
     ...

然后在你的控制器中:

var AlertModalInstanceCtrl = function ($scope, $modalInstance, data) {
    $scope.$on('modal:start-open', function() { 
        $modalInstance.dismiss('cancelled'); 
    });
    ...