我是 ngx-bootstrap 的新手.. 使用 ngx-bootstrap 模态..
使用子模态组件 (@ViewChild('childModal') childModal: CommonModalComponent)
我能够创建简单的模态。
要求是在同一页面上显示多个可拖动模式。
例如。页面上的两个按钮。按钮 1 和按钮 2。如果我单击 button1,则应该显示相应的 modal-1。现在modal-1应该保持打开状态,我可以点击button2,它会显示modal2。现在我可以在同一个页面上同时看到 modal1 和 modal2 并且能够同时拖动它们......?
如何用 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">×</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>
父组件.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();
}
}
父组件.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">×</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">×</span>
</button>
</div>
</common-modal>