2

i am new in ngx-bootstrap.. working with ngx-bootstrap modals..

using child modal component (@ViewChild('childModal') childModal: CommonModalComponent)

i am able to create simple modals.

the requirement is to show multiple draggable modals at the same page.

Eg. two buttons on page. button1 & button2. if I click on button1 than respective modal-1 should show. and now modal-1 should remain open and I can click on button2 and it will show modal2. now I can see both modal1 and modal2 on the same page and able to drag both..?

how to do it with ngx-bootstrap?

modal-dailog.component.ts

import {Component,Input, ViewChild} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';

@Component({
  selector: 'common-modal',
  templateUrl: './modal-dailog.component.html',

})
export class CommonModalComponent {
   @ViewChild('childModal') public childModal:ModalDirective;
   @Input() title?:string;
  constructor() {
  }
  show(){
    this.childModal.show();
  }
  hide(){
    this.childModal.hide();
  }
}

modal-dailog.component.html

<div id='wrapper'>

    <div bsModal #childModal="bs-modal" class="modal fade"  role="dialog" aria-labelledby="mySmallModalLabel" [config]="{backdrop: false, ignoreBackdropClick: true}"  aria-hidden="false">
         <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title pull-left">{{title}}</h4>
                    <button type="button" class="close pull-right" aria-label="Close" (click)="hide()">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <ng-content select=".modal-body"> </ng-content>
                    <ng-content select=".modal-footer"> </ng-content>
                </div>

            </div>
        </div>
    </div>
</div>

parent.component.ts

import {Component, ViewChild, ViewContainerRef} from '@angular/core'
import {CommonModalComponent} from './core/modal-dailog/modal-dailog.component';
@Component({
    selector: 'modal-demo',
    templateUrl: './parent.component.html',
})
export class demoComponent {

    @ViewChild('childModal') childModal: CommonModalComponent;
    @ViewChild('childModal2') childModal2: CommonModalComponent;
    constructor() {
    } 
    onCancle() {        
        this.childModal.hide();
    }

    onCancle2() {

        this.childModal2.hide();
    }

}

parent.component.html

        <button type="button" class="btn btn-primary" 
           (click)="childModal.show()">Open modal</button>

        <common-modal  #childModal [title]="'common modal'"> 
            <div class="modal-body">
               hii, modal-body 1
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" 
                  (click)="onCancle()" >Cancle
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>  
        </common-modal> 


        <button type="button" class="btn btn-primary" 
           (click)="childModal2.show()">Open modal2</button>
        <common-modal  #childModal2 [title]="'common modal2'"> 
            <div class="modal-body">
               hii, modal-body 2
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" 
                  (click)="onCancle2()" >Cancle
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>  
        </common-modal>

0 Answers0