Angular 2&ngBootstrap – 共享模态

前端之家收集整理的这篇文章主要介绍了Angular 2&ngBootstrap – 共享模态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以将ngBootstrap模式作为共享组件并在其他组件中使用它的实例.

我想有一个Modal提示用户删除一条记录,我想在不同记录类型的多个组件中重复使用它.

ngBootstrap站点显示“组件为内容”示例,但在该示例中,看起来ModalComponent指示是打开还是关闭ModalContents.我希望能够从另一个(任意)组件打开/关闭模态的实例.

这可能吗?

>创建一个CommonModule,如下所示,
  1. import { NgModule} from '@angular/core';
  2. import { CommonModalComponent } from './modal.component';
  3. import { ModalDirective,ModalModule } from 'ng2-bootstrap/ng2-bootstrap';
  4. @NgModule({
  5. imports:[ModalModule],declarations: [ CommonModalComponent ],exports:[CommonModalComponent]
  6. })
  7. export class CommonModule {}

>如您所见,CommonModalComponent是一个声明
CommonModule,创建如下组件,

  1. import {Component,Input,ViewChild} from '@angular/core';
  2. import { ModalDirective } from 'ng2-bootstrap/ng2-bootstrap';
  3.  
  4. @Component({
  5. selector: 'common-modal',template: `
  6. <div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  7. <div class="modal-dialog modal-sm">
  8. <div class="modal-content">
  9. <div class="modal-header">
  10. <h4 class="modal-title pull-left">{{title}}</h4>
  11. <button type="button" class="close pull-right" aria-label="Close" (click)="hideChildModal()">
  12. <span aria-hidden="true">&times;</span>
  13. </button>
  14. </div>
  15. <div class="modal-body">
  16. <ng-content select=".modal-body"> </ng-content>
  17. </div>
  18.  
  19. <div class="modal-footer">
  20. <div class="pull-left">
  21. <button class="btn btn-default" (click)="hide()"> Cancel </button>
  22. </div>
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. `,})
  28. export class CommonModalComponent {
  29. @ViewChild('childModal') public childModal:ModalDirective;
  30. @Input() title?:string;
  31. constructor() {
  32. }
  33. show(){
  34. this.childModal.show();
  35. }
  36. hide(){
  37. this.childModal.hide();
  38. }
  39. }

>使用AppModule中的CommonModule及其组件,ViewChild,ViewContainerRef,NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {CommonModule} from './common.module'; import {CommonModalComponent} './modal.component'; @Component({ selector: 'my-app',template: ` <div> <h2>Hello {{name}}</h2> <button type="button" class="btn btn-primary" (click)="childModal.show()">Open modal</button> <common-modal #childModal [title]="'common modal'"> </common-modal> </div> `,}) export class App { @ViewChild('childComponent') childComponent:CommonModalComponent; name:string; constructor(private viewContainerRef: ViewContainerRef) { this.name = 'Angular 2 Common Module with ModalComponent' } } @NgModule({ imports: [ BrowserModule,CommonModule ],declarations: [ App ],bootstrap: [ App ] }) export class AppModule {}

LIVE DEMO

附加信息:

>这个通用模块可以用于任何重现您需求的地方
在Angular 2中的ContentProjection的帮助下可以看到
在线以下

  1. <ng-content select=".modal-body"> </ng-content>

>在AppComponent中,您可以使用此项并将项目添加到您的
CommonModal为,

  1. <div class="modal-body">
  2. Some Form Controls or Displaying content
  3. </div>

这可以在LIVE DEMO中看到>因为,你想要模态警告信息或确认重用common-modal并创建另一个WarningModalComponent并跨应用程序使用它

猜你在找的Angularjs相关文章