114

I am creating a modal:

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

is there a way to increase its width?

Ivan Bacher
  • 5,855
  • 9
  • 36
  • 56
  • possible duplicate of [How can I change the width of a Bootstrap 3 modal in IE8?](http://stackoverflow.com/questions/18346203/how-can-i-change-the-width-of-a-bootstrap-3-modal-in-ie8) – JohnnyHK Jan 23 '14 at 15:03
  • 1
    You can always change the width using css. – Alex Choroshin Jan 23 '14 at 15:39

11 Answers11

233

I use a css class like so to target the modal-dialog class:

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

Then in the controller calling the modal window, set the 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;
            });
    };
Rob J
  • 6,609
  • 5
  • 28
  • 29
  • 6
    This is great, I didn't know it must be also applied to `.modal-dialog` – Jaanus Jan 30 '14 at 09:12
  • Css should be applied on both .app-modal-window and .modal-dialog, so: .app-modal-window, .modal-dialog { width: 500px; } – Alexander Jun 19 '14 at 15:00
  • 2
    @Alexander Not according to my tests, adding a width to the parent element of the modal-dialog screws it all up. – Rob J Jun 19 '14 at 17:50
  • @RobJacobs sorry, I didn't mention I use boostrap 2.3.2. I guess this is the reason I should set width for both .app-modal-window and .modal-dialog – Alexander Jun 22 '14 at 08:19
  • Beware angular 'C' directives (classes) mentioned in `windowClass` won't work. For example, if you have a directive `"user-defined-width"`, `windowClass: "user-defined-width"` does nothing. [example](http://plnkr.co/edit/73TQCXgG9DQDERqTOEB7) – Zero Distraction Aug 05 '14 at 04:48
  • 1
    @ZeroDistraction The windowClass defined is added to the element class attribute in the modalWindow directive - link function which is after the compile phase, hence the 'C' directive does not get compiled. – Rob J Aug 05 '14 at 12:19
  • Yes, and I ended up implementing a `modalWindow` directive that inspects the dialog attribute window-class, so I can write my directives on that. Implementing the directive was suggested from the bootstrap issue [#1050](https://github.com/angular-ui/bootstrap/issues/1057) in [here](http://plnkr.co/edit/7jbo7mkUEtcMbpdZGyhj?p=preview). – Zero Distraction Aug 06 '14 at 06:23
  • @RobJ Thanks for the solution :) What about after fixing the modal size, it is not 'responsive' as it is before any more? – Roy Lee Aug 13 '14 at 07:50
  • @Rob J: Funny, you suggested something for a new question of mine, and I had this one with your answer open from last night. – VSO Jul 16 '15 at 14:03
  • Unhelpful to people using the modal in multiple areas. Also does not answer the question specifically – Sam Alexander Dec 02 '16 at 21:55
49

Another easy way is to use 2 premade sizes and pass them as parameter when calling the function in your html. use: 'lg' for large modals with width 900px 'sm' for small modals with width 300px or passing no parameter you use the default size which is 600px.

example code:

$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;
        });
};

and in the html I would use something like the following:

<button ng-click="openModal('lg')">open Modal</button>  
Daniele Urania
  • 2,658
  • 1
  • 15
  • 11
31

You can specify custom width for .modal-lg class specifically for your popup for wider viewport resolution. Here is how to do it:

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'
});
Dmitry Komin
  • 549
  • 6
  • 7
17

there is another way wich you don't have to overwrite uibModal classes and use them if needed : you call $uibModal.open function with your own size type like "xlg" and then you define a class named "modal-xlg" like below :

.modal-xlg{
   width:1200px;
}

call $uibModal.open as :

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

and this will work . because whatever string you pass as size bootstrap will cocant it with "modal-" and this will play the role of class for window.

13

When we open a modal it accept size as a paramenter:

Possible values for it size: 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>

If you want any specific size, add style on model HTML:

<style>.modal-dialog {width: 500px;} </style>
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
3

I found it easier to just take over the template from Bootstrap-ui. I have left the commented HTML still in-place to show what I changed.

Overwrite their default template:

<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>

Modify your dialog template (note the wrapper DIVs containing "modal-dialog" class and "modal-content" class):

<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>

And then call the modal with whatever CSS class or style parameters you wish to change (assuming you have already defined "app" somewhere else):

<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>

Add an "open" button and fire away.

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

Now you can add whatever extra class you want, or simply change width/height sizes as necessary.

I further enclosed it within a wrapper directive, which is should be trivial from this point forward.

Cheers.

Roman
  • 631
  • 1
  • 13
  • 14
3

I solved the problem using Dmitry Komin solution, but with different CSS syntax to make it works directly in browser.

CSS

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

JS is the same:

var modal = $modal.open({
    animation: true,
    templateUrl: 'modalTemplate.html',
    controller: 'modalController',
    size: 'lg',
    windowClass: 'my-modal'
});
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
3

You can do this in very simple way using size property of angular modal.

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

Use max-width on modal-dialog for angular 5

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

and use windowClass as others recommended, TS eg:

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

Also, I had to put the css code in global styles > styles.css.

ajsanjh
  • 11
  • 2
0

If you want to just go with the default large size you can add 'modal-lg':

var modal = $modal.open({
          templateUrl: "/partials/welcome",
          controller: "welcomeCtrl",
          backdrop: "static",
          scope: $scope,
          windowClass: 'modal-lg'
});
alexoviedo999
  • 6,761
  • 1
  • 26
  • 17
0

1/ in a first time, you can create a modal.component.ts file and define a function open() as here

import { Component, Input, TemplateRef, ViewChild } from '@angular/core';
import { ModalConfig } from '../modal.config';
import { NgbModal, NgbModalOptions, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
})
export class ModalComponent {
  @Input() public modalConfig: ModalConfig;
  @ViewChild('modal') private modalContent: TemplateRef<ModalComponent>;
  private modalRef: NgbModalRef;

  constructor(private modalService: NgbModal) {}

  open(options?: NgbModalOptions): Promise<boolean> {
    return new Promise<boolean>((resolve) => {
      this.modalRef = this.modalService.open(this.modalContent, options);
      this.modalRef.result.then(resolve, resolve);
    });
  }
}

2/ Then you have to use this function in your other component or you want to make a modal call. for example in my case I make a modal call in my dashboard.component.ts

import { Component, TemplateRef, ViewChild } from '@angular/core';
import { ModalConfig, ModalComponent, ModalSize } from '../../_pressing/partials';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss'],
})
export class DashboardComponent
{
  modalConfig: ModalConfig = {
    modalTitle: 'Modal title',
    dismissButtonLabel: 'Submit',
    closeButtonLabel: 'Cancel',
    size: ModalSize.Large,
  };

  @ViewChild('modal') private modalComponent: ModalComponent;
  @ViewChild('content') private modalContent: TemplateRef<ModalComponent>;

  constructor(private modalService: NgbModal) { }

  async openModal() {
    return await this.modalComponent.open({
      modalDialogClass: 'dark-modal mw-650px',
      fullscreen: 'lg',
      size: 'lg',
    });
  }
}

In this way, you can use a component in several files by adapting the configurations.

hope this helps someone sorry for my English