组件之间共享Angular 2模态

时间:2016-12-20 17:42:30

标签: angular data-binding modal-dialog bootstrap-modal sharing

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

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

我将它发布在app.ts文件下面:

2016年12月21日更新
按照@echonax的建议,我更新了我的插件:

//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{}

敬请关注!

2 个答案:

答案 0 :(得分:2)

那个plunker的原始版本实际上是我的问题:How to dynamically create bootstrap modals as Angular2 components?

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

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

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

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

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

答案 1 :(得分:2)

我知道这个话题已经有6个月了,但实现这一目标对我来说真的很重要。

所以我创建了一个npm模块,允许在共享数据和远程打开/关闭/事件的组件之间共享模式。

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

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

与Angular&gt; = 2.0.0

兼容