ng-bootstrap Modal size

前端 未结 9 2129
抹茶落季
抹茶落季 2020-12-29 18:25

What is the best way to set/override a custom width for a modal?

It seems ng-bootstrap currently supports

size: \'sm\' | \'lg\'

相关标签:
9条回答
  • 2020-12-29 18:51

    add following css in style.css file.

    .xlModal > .modal-dialog {
        max-width: 1490px !important;
        min-width: 971px !important;   
        width: 95% !important;
    }
    

    Note : we can replace .xlModal with any name.

    open modal with new created style.

    this.modalService.open(content,{windowClass:"xlModal"});
    
    0 讨论(0)
  • 2020-12-29 18:59

    I found this as a good solution.

    this.modalService.open(MyComponent, { windowClass: 'my-class'});
    

    ng-deep “shadow-piercing” descendant combinator can be used to force a style down through the child component tree into all the child component views. In this case modal-dialog class.

    ::ng-deep .my-class .modal-dialog { 
         max-width: 80%;
         width: 80%;
    }
    
    0 讨论(0)
  • 2020-12-29 19:02

    You could wrap it into a function like

    component.ts

    // accepts 'sm', 'md' or 'lg' as argument.
    // default argument 'md'
    public openModal( dialogSize: 'sm' | 'md' | 'lg' = 'md'): void {
      let modalOptions = {};
      if (dialogSize === 'sm' || dialogSize === 'lg') {
        modalOptions = { size: dialogSize };
      } else {
        modalOptions = { windowClass: 'md-class' };
      }
      this.modalService.open(ConfirmationDialogComponent, modalOptions);
    }
    

    component.css

    ::ng-deep .md-class .modal-dialog { 
        max-width: 80%;
        width: 80%;
    }
    

    And call it like,

    openModal(); //argument 'md'
    openModal('sm'); //argument 'sm'
    openModal('md'); //argument 'md'
    openModal('lg'); //argument 'lg'
    
    0 讨论(0)
  • 2020-12-29 19:05

    When you don't specify the size, the default value use $modal-md (500px).

    All the 3 modals sm, md and lg are responsive and will display on full width (with borders) on mobile.

    If you really want a new size, you can use a custom size:

    this.modalService.open(MyModalComponent, { size: <any>'xl' });
    

    With a css for this class:

    .modal-xl {
      max-width: 1000px;
    }
    

    EDIT: the xl size is now a standard size so the CSS.

    0 讨论(0)
  • 2020-12-29 19:09

    Add a argument class: 'modal-lg'

    open() {
            const modalRef = this.modalService.open(ModelComponent, { class: 'modal-lg', backdrop: 'static' });
        }
    
    }
    
    0 讨论(0)
  • 2020-12-29 19:10

    Try like this :

    import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
    import { ModelComponent } from './model.component';
    
    export class Component {
    
        constructor(private modalService: NgbModal) {}
    
        open() {
            const modalRef = this.modalService.open(ModelComponent, { size: 'lg', backdrop: 'static' });
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题