npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngb-modal

v2.0.3

Published

Modal dialog for angular applications using bootstrap3

Downloads

2,547

Readme

Angular Bootstrap Modal

Modal dialog for angular 6 applications using bootstrap 4. If used without bootstrap, please add appropriate styles for modal to work properly. Raise an issue on github if found any. Please star the project if you found it useful.

Installation

npm install --save ngb-modal

For Angular[2,4,5] and bootstrap 3, install older version

npm install --save [email protected]

For Angular 6 and bootstrap 3, install older version

npm install --save [email protected]

Demo

Check working Demo. You can test and generate code snippet from demo.

Usage

Import ModalModule into your module

your-module.module.ts

import { ModalModule } from 'ngb-modal';

@NgModule({
  imports: [
    ...
    ModalModule,
  ],

Now Start using modal component in your components.

your-component.component.html

<modal #myModal>
    <modal-header>
        Modal header content goes there.
    </modal-header>

    <modal-content>
        Modal body content goes there.
    </modal-content>

    <modal-footer>
        Modal footer content goes there.
    </modal-footer>
</modal>

Properties

* title?: String;
* size?: String | "md";
* modalClass?: String | '';
* hideCloseButton?: Boolean | false;
* centered?: Boolean | false;
* backdrop?: Boolean | 'static' | true;
* animation?: Boolean | true;
* keyboard?: Boolean | true;
* closeOnOutsideClick?: Boolean | true;
* backdropClass?: String | "modal-backdrop";

Note: These properties can be passed in html on modal tag or pass as an 2nd parameter in open method of ModalManager when opening a modal as shown below.

Opening a Modal

your-component.ts

import { ModalManager } from 'ngb-modal';

@Component({
selector: "app",
template: `
    <div class="row">
        <button (click)="openModal()">open my modal</button>
        <modal #myModal (onOpen)="" (onClose)="">
            <modal-header>
                <h1>Modal header</h1>
            </modal-header>
            <modal-content>
                Hello Modal!
            </modal-content>
            <modal-footer>
                <button class="btn btn-primary" (click)="closeModal()">close</button>
            </modal-footer>
        </modal>
    </div>`
})
export class YourComponent {
    @ViewChild('myModal') myModal;
    private modalRef;
    constructor(private modalService: ModalManager){}

    openModal(){
        this.modalRef = this.modalService.open(this.myModal, {
            size: "md",
            modalClass: 'mymodal',
            hideCloseButton: false,
            centered: false,
            backdrop: true,
            animation: true,
            keyboard: false,
            closeOnOutsideClick: true,
            backdropClass: "modal-backdrop"
        })
    }
    closeModal(){
        this.modalService.close(this.modalRef);
        //or this.modalRef.close();
    }
}

Separate component as modal:

You can make a separate component for modal window. In this case remember to add your component as an entryComponents section of your NgModule. Also set ViewContainerRef in your root component as below.


@NgModule({
  declarations: [
    AppComponent, ModalComp
  ],
  imports: [
    ...
    ModalModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [ModalComp]
})
export class AppModule { }

export class AppComponent {
    constructor(private modalService: ModalManager, private vcr: ViewContainerRef) {
        this.modalService.setDefaults({
        title: "new modal",
        size: "sm",
        modalClass: 'mymodal',
        hideCloseButton: true,
        centered: false,
        backdrop: true,
        animation: true,
        keyboard: true,
        closeOnOutsideClick: true,
        backdropClass: "modal-backdrop"
        })
        this.modal.setRootViewContainerRef(this.vcr);
    }
}

import { ModalComponent } from 'ngb-modal';

@Component({
  selector: 'app-modal',
  template: `<modal>
  <modal-header>
      Modal as a component
  </modal-header>

  <modal-content>
      <span>{{data}}</span>
      Modal body content goes there.
  </modal-content>

  <modal-footer>
      Modal footer content goes there.
  </modal-footer>
</modal>`
})
export class ModalComp{
    @ViewChild(ModalComponent) ModalComponent;
    @Input() data;
}

export class YourComponent {
    private modalRef;
    constructor(private modalService: ModalManager){}

    openModal(){
        this.modalRef = this.modalService.open(ModalComp, {
            size: "md",
            modalClass: 'mymodal',
            hideCloseButton: false,
            centered: false,
            backdrop: true,
            animation: true,
            keyboard: false,
            closeOnOutsideClick: true,
            backdropClass: "modal-backdrop"
        });
        this.modalRef['data'] = "any data you want to pass to ModalComp class";
        this.modalRef.onOpen.subscribe(() => {
            console.log("comp opened");
        })
        this.modalRef.onClose.subscribe(() => {
            console.log("comp closed");
        })
    }
    closeModal(){
        this.modalService.close(this.modalRef);
        //or this.modalRef.close();
    }
}

Note: If you are using another component as modal. Don't forget to add @ViewChild(ModalComponent) ModalComponent in your component as above. ModalManager expects ModalComponent property to be present on your component class. Don't change the name. Any input property of your component can be passed to modalInstance returned by open method.

API

ModalManager expose 4 methods to component to control the modal.

* open: Method // used open a particular modal. 1st argument is modalRefence you want to open,2nd is config you want to be applied on this modal.It returns instance of opened modal, which has 3 properties:
    * onOpen : Observable // fired once this modal is opened
    * onClose : Observable // fired once this modal is closed
    * close : Method // can be used to close this modal

* close : Method //Used to close any opened modal by passing the reference of that modal.
* setDefaults : Method //to be called from root component to provide global configurations for all modals.
* setRootViewContainerRef: Method //called if using separate component as modal. called from root component to set container for all modals at root level.

Output

These 2 output events are published on <modal></modal> component.

onOpen : Fired after modal is opened. onClose : Fired after modal is closed

Global Configuration

If you want to set some default properties for all modals used in project, these configurations can be set via setDefaults method of ModalManager. Call setDefaults from your root component and the configurations.

License

MIT © Uttam Pratap Choudhary