繁体   English   中英

组件之间共享的Angular 2模态

[英]Angular 2 modals shared between components

我有一个具有许多组件的典型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{}

敬请关注!

这个问题的原始版本实际上是我的问题: 如何动态创建Bootstrap模态作为Angular2组件?

@Günter给出了一个了不起的答案,我认为这确实被低估了。

窃听器有一个小错误,您可以在此处找到固定版本: https ://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview

如果案例引用了错误的字段,请更改

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兼容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM