【问题标题】:Angular 2 modals shared between componentsAngular 2 模态在组件之间共享
【发布时间】:2017-05-06 00:29:55
【问题描述】:

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

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

我把它贴在我的 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 的原始版本实际上是我的问题:stackoverflow.com/questions/36566698/… 和 @Günter 给出了一个惊人的答案,我认为这真的被低估了。它有一个小错误,我会告诉 Günter 但你可以在这里找到修复的版本:plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview
  • @echonax 非常感谢!我尝试在我的代码中实现它,我会告诉你的。
  • 好吧,因为 SharedService 服务必须写成这样:showModal:Subject&lt;any&gt; = new Subject(); ;)

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


【解决方案1】:

那个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();
}

【讨论】:

    【解决方案2】:

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

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

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

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

    兼容 Angular >=2.0.0

    【讨论】:

    • 非常感谢!我会尝试将它用于我的以下脚本。
    • 很高兴!随意创建一些问题来帮助我改进它;)
    • 更新:我更新了组件以修复 AOT 构建中的问题。我还编辑了 repo 的链接,因为它移动了 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多