Angular 2 个模态在组件之间共享

Angular 2 modals shared between components

我有一个典型的 Angular 2 应用程序,其中包含许多组件。
我安装了这个模态组件:https://github.com/pleerock/ng2-modal.
有没有办法在更多组件之间共享相同的模态? 我解释得更好。我希望在单击不同组件内的不同按钮时应该打开相同的模式。可能吗?

我试过这种方法 https://plnkr.co/edit/RgI1e9PobC7FloLHpEFD?p=preview 但这不是正确的方法,因为它总是向模态添加内容。

我 post 它在我的 app.ts 文件下面:

21/12/2016 更新
按照@echonax 的建议,我更新了我的 plunk:

//our root app component
import {
  NgModule, 
  ComponentRef, 
  Injectable, 
  Component, 
  Injector, 
  ViewContainerRef, 
  ViewChild, ComponentFactoryResolver} from "@angular/core";
import {BrowserModule} from '@angular/platform-browser'
import {Subject} from 'rxjs/Subject';

@Injectable()
export class SharedService {
  showModal:Subject<any> = new Subject();
}


@Component({
  selector: 'child-component',
  template: `
      <button (click)="showDialog()">show modal from child</button>
  `,
})
export class ChildComponent {
  constructor(private sharedService:SharedService) {}

  showDialog() {
    this.sharedService.showModal.next(CompComponent);
  }

}


@Component({
  selector: 'comp-comp',
  template: `MyComponent`
})
export class CompComponent { }


@Component({
  selector: 'modal-comp',
  template: `
  <div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="theModalLabel">
    <div class="modal-dialog largeWidth" role="document">
      <div class="modal-content">
        <div class="modal-header">
        <h4 class="modal-title" id="theModalLabel">The Label</h4></div>
        <div class="modal-body" #theBody>
      </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" (close)="close()">Close</button>
  </div></div></div></div>
`
})
export class ModalComponent {
  @ViewChild('theBody', {read: ViewContainerRef}) theBody;

  cmp:ComponentRef<any>;
  cmpRef:ComponentRef<any>;

  constructor(
    sharedService:SharedService, 
    private componentFactoryResolver: ComponentFactoryResolver, 
    injector: Injector) {

    sharedService.showModal.subscribe(type => {
      if(this.cmp) {
        this.cmp.destroy();
      }
      let factory = this.componentFactoryResolver.resolveComponentFactory(type);
      this.cmpRef = this.theBody.createComponent(factory)
      $('#theModal').modal('show');
    });
  }

  close() {
    if(this.cmp) {
      this.cmp.destroy();
    }
    this.cmp = null;
  }
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello</h2>
      <button (click)="showDialog()">show modal</button>
      <child-component></child-component>
    </div>
  `,
})
export class App {
  constructor(private sharedService:SharedService) {}

  showDialog() {
    this.sharedService.showModal.next(CompComponent);
  }

}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, ModalComponent, CompComponent, ChildComponent],
  providers: [SharedService],
  entryComponents: [CompComponent],
  bootstrap: [ App, ModalComponent ]
})
export class AppModule{}

敬请期待!

那个plunker的原始版本实际上是我的问题:

@Günter 给出了一个惊人的答案,我认为这个答案真的被低估了。

plunker有一个小错误,你可以在这里找到修复版本:https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview

if 案例引用了错误的字段,因此更改

if(this.cmp) {
    this.cmp.destroy();
}

if(this.cmpRef) {
    this.cmpRef.destroy();
}

我知道这个主题已有 6 个月大,但实现这样的目标对我来说真的很划算。

所以我制作了一个 npm 模块,允许具有共享数据和远程打开/关闭/事件的组件之间进行模式共享。

随时查看:https://github.com/biig-io/ngx-smart-modal

演示在这里:https://biig-io.github.io/ngx-smart-modal/

兼容 Angular >=2.0.0