如何在Angular UI Bootstrap中增加模态宽度?

时间:2014-01-23 14:59:33

标签: angularjs angular-ui-bootstrap

我正在创建一个模态:

var modal = $modal.open({
                    templateUrl: "/partials/welcome",
                    controller: "welcomeCtrl",
                    backdrop: "static",
                    scope: $scope,
                });

有没有办法增加它的宽度?

10 个答案:

答案 0 :(得分:215)

我使用像这样的css类来定位模态对话框类:

.app-modal-window .modal-dialog {
  width: 500px;
}

然后在调用模态窗口的控制器中,设置windowClass:

    $scope.modalButtonClick = function () {
        var modalInstance = $modal.open({
            templateUrl: 'App/Views/modalView.html',
            controller: 'modalController',
            windowClass: 'app-modal-window'
        });
        modalInstance.result.then(
            //close
            function (result) {
                var a = result;
            },
            //dismiss
            function (result) {
                var a = result;
            });
    };

答案 1 :(得分:45)

另一个简单的方法是使用2个预制尺寸,并在调用html中的函数时将它们作为参数传递。 使用: ' LG'适用于宽度为900px的大型模态 ' SM'适用于宽度为300px的小型模态 或者不传递参数,使用默认大小600px。

示例代码:

$scope.openModal = function (size) {
var modal = $modal.open({
                templateUrl: "/partials/welcome",
                controller: "welcomeCtrl",
                backdrop: "static",
                scope: $scope,
                size: size,
            });
modal.result.then(
        //close
        function (result) {
            var a = result;
        },
        //dismiss
        function (result) {
            var a = result;
        });
};

在html中我会使用类似下面的内容:

<button ng-click="openModal('lg')">open Modal</button>  

答案 2 :(得分:25)

您可以指定.modal-lg类的自定义宽度,专门用于弹出窗口以获得更宽的视口分辨率。以下是如何做到这一点:

CSS:

@media (min-width: 1400px){

   .my-modal-popup .modal-lg {
       width: 1308px;
   }       

}

JS:

var modal = $modal.open({
    animation: true,
    templateUrl: 'modalTemplate.html',
    controller: 'modalController',
    size: 'lg',
    windowClass: 'my-modal-popup'
});

答案 3 :(得分:12)

当我们打开一个模态时,它接受大小作为参数:

尺寸的可能值:sm, md, lg

$scope.openModal = function (size) {
var modal = $modal.open({
      size: size,
      templateUrl: "/app/user/welcome.html",
      ...... 
      });
}

HTML:

<button type="button" 
    class="btn btn-default" 
    ng-click="openModal('sm')">Small Modal</button>

<button type="button" 
    class="btn btn-default" 
    ng-click="openModal('md')">Medium Modal</button>

<button type="button" 
    class="btn btn-default" 
    ng-click="openModal('lg')">Large Modal</button>

如果您想要任何特定尺寸,请在型号HTML上添加样式:

<style>.modal-dialog {width: 500px;} </style>

答案 4 :(得分:10)

还有另外一种方法,你不必覆盖uibModal类并在需要时使用它们:你用自己的大小类型调用$ uibModal.open函数,如&#34; xlg&#34;然后你定义一个名为&#34; modal-xlg&#34;的类。如下所示:

.modal-xlg{
   width:1200px;
}

将$ uibModal.open称为:

 var modalInstance = $uibModal.open({
                ...  
                size: "xlg",
            });

这将有效。 因为你传递的任何字符串作为大小引导程序都会使用&#34; modal - &#34;这将扮演窗口类的角色。

答案 5 :(得分:3)

我发现从Bootstrap-ui接管模板更容易。我已将评论的HTML留在原地以显示我更改的内容。

覆盖默认模板:

<script type="text/ng-template" id="myDlgTemplateWrapper.html">
    <div tabindex="-1" role="dialog" class="modal fade" ng-class="{in: animate}" 
         ng-style="{'z-index': 1050 + index*10, display: 'block'}" ng-click="close($event)"> 
        <!-- <div class="modal-dialog"
            ng-class="{'modal-sm': size == 'sm', 'modal-lg': size == 'lg'}"
            >
            <div class="modal-content"  modal-transclude></div>
        </div>--> 

        <div modal-transclude>
            <!-- Your content goes here -->
        </div>
    </div>
</script>

修改对话框模板(注意包含“模态对话框”类和“模态内容”类的包装器DIV):

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-dialog {{extraDlgClass}}"
         style="width: {{width}}; max-width: {{maxWidth}}; min-width: {{minWidth}}; ">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h3>I am a more flexible modal!</h3>
            </div>
            <div class="modal-body"
                 style="min-height: {{minHeight}}; height: {{height}}; max-height {{maxHeight}}; ">
                <p>Make me any size you want</p>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" ng-click="ok()">OK</button>
                <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
            </div>
        </div>
    </div>
</script>

然后使用您希望更改的任何CSS类或样式参数调用模态(假设您已在其他位置定义了“app”):

<script type="text/javascript">
    app.controller("myTest", ["$scope", "$modal", function ($scope, $modal)
    {
        //  Adjust these with your parameters as needed

        $scope.extraDlgClass = undefined;

        $scope.width = "70%";
        $scope.height = "200px";
        $scope.maxWidth = undefined;
        $scope.maxHeight = undefined;
        $scope.minWidth = undefined;
        $scope.minHeight = undefined;

        $scope.open = function ()
        {
            $scope.modalInstance = $modal.open(
            {
                backdrop: 'static',
                keyboard: false,
                modalFade: true,

                templateUrl: "myModalContent.html",
                windowTemplateUrl: "myDlgTemplateWrapper.html",
                scope: $scope,
                //size: size,   - overwritten by the extraDlgClass below (use 'modal-lg' or 'modal-sm' if desired)

                extraDlgClass: $scope.extraDlgClass,

                width: $scope.width,
                height: $scope.height,
                maxWidth: $scope.maxWidth,
                maxHeight: $scope.maxHeight,
                minWidth: $scope.minWidth,
                minHeight: $scope.minHeight
            });

            $scope.modalInstance.result.then(function ()
            {
                console.log('Modal closed at: ' + new Date());
            },
            function ()
            {
                console.log('Modal dismissed at: ' + new Date());
            });
        };

        $scope.ok = function ($event)
        {
            if ($event)
                $event.preventDefault();
            $scope.modalInstance.close("OK");
        };

        $scope.cancel = function ($event)
        {
            if ($event)
                $event.preventDefault();
            $scope.modalInstance.dismiss('cancel');
        };

        $scope.openFlexModal = function ()
        {
            $scope.open();
        }

    }]);
</script>

添加一个“打开”按钮并开火。

    <button ng-controller="myTest" class="btn btn-default" type="button" ng-click="openFlexModal();">Open Flex Modal!</button>

现在您可以添加所需的任何额外课程,或者只需根据需要更改宽度/高度尺寸。

我进一步将它包含在一个包装器指令中,从这一点开始应该是微不足道的。

干杯。

答案 6 :(得分:3)

我使用Dmitry Komin解决方案解决了这个问题,但使用不同的CSS语法使其直接在浏览器中运行。

CSS

@media(min-width: 1400px){
    .my-modal > .modal-lg {
        width: 1308px;
    }
}

JS是一样的:

var modal = $modal.open({
    animation: true,
    templateUrl: 'modalTemplate.html',
    controller: 'modalController',
    size: 'lg',
    windowClass: 'my-modal'
});

答案 7 :(得分:2)

  

您可以使用angular的size属性以非常简单的方式执行此操作   模态的。

var modal = $modal.open({
                    templateUrl: "/partials/welcome",
                    controller: "welcomeCtrl",
                    backdrop: "static",
                    scope: $scope,
                    size:'lg' // you can try different width like 'sm', 'md'
                });

答案 8 :(得分:1)

如果您想使用默认的大尺寸,可以添加'modal-lg':

var modal = $modal.open({
          templateUrl: "/partials/welcome",
          controller: "welcomeCtrl",
          backdrop: "static",
          scope: $scope,
          windowClass: 'modal-lg'
});

答案 9 :(得分:0)

对模态对话框使用最大宽度为角度5

.mod-class .modal-dialog {
    max-width: 1000px;
}

并使用其他推荐的windowClass,例如:

this.modalService.open(content, { windowClass: 'mod-class' }).result.then(
        (result) => {
            // this.closeResult = `Closed with: ${result}`;
         }, (reason) => {
            // this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});

此外,我不得不将css代码放入全局样式> styles.css。