如何打开作为子组件的 ng-Bootstrap-Modal?

Fed*_*ete 4 bootstrap-modal ng-bootstrap angular-components angular ng-bootstrap-modal

我是 Angular 的新手,对ng-Bootstrap 的Modals有一些疑问。我已经能够打开同一组件中的模态框,并且它们工作正常,一个例子是:

关联:

<a (click)="openAbout(contentAbout)" class="nav-link">About</a>
Run Code Online (Sandbox Code Playgroud)

点击事件:

  openAbout(contentAbout) {
    this.modalService.open(contentAbout, { centered: true, scrollable: true });
  }
Run Code Online (Sandbox Code Playgroud)

模态:

<ng-template #contentAbout let-c="close" let-d="dismiss">
  <div class="modal-header">
      <h4 class="modal-title" id="modal-primary-title">About us</h4>
      <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
  </div>
  <div class="modal-body centered">
    <h2>Gravity Now!</h2>
    <p>It is a platform designed for Space Apps Challenge and the Challenge Gravity Map, Earth Watch category and was chosen in the Top 5 of The Most Inspiring Projects of 2014.</p>
    <a href="https://2014.spaceappschallenge.org/awards/#globalawards" target="_blank">
      <img id="spaceLogo" src="./assets/space_apps.png">
    </a>
    <br>
    <img id="supernovaLogo" src="./assets/supernova-logo.png">
    <p>Also, you can download our apps.</p>
    <div class="row">
      <div class="col">
        <a href="https://play.google.com/store/apps/details?id=tk.supernova.gnow" target="_blank">
          <img class="logo" src="./assets/android.png">
        </a>
      </div>
      <div class="col">
        <a href="https://www.microsoft.com/en-us/p/gravity-now/9nblgggzjlp5" target="_blank">
          <img class="logoWindows" src="./assets/windows.png">
        </a>
      </div>
    </div>
  </div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

它有效,但是,我想将此模态移动到子组件,因为我有 4 个模态,并且代码看起来有些混乱,但是如果我创建一个新的子组件,将模态代码移动到子组件,然后,我将其添加到父组件中,如下所示:

<app-about></app-about>
Run Code Online (Sandbox Code Playgroud)

没有任何反应,因为单击不会打开任何内容。有人有类似经历吗?你知道我应该在点击事件中更改什么吗?

我什至阅读了文档,但找不到与我的文档相关的示例:

https://ng-bootstrap.github.io/#/components/modal/examples

谢谢你的任何想法。

Fed*_*ete 5

有一些变化需要考虑。

第一步是在entryComponents以下部分注册您的模态框app.module.ts

entryComponents: [
    AboutComponent,
],
Run Code Online (Sandbox Code Playgroud)

接下来,将旧模态框复制到新组件并删除以下两行:

<ng-template #contentAbout let-c="close" let-d="dismiss">
</ng-template>
Run Code Online (Sandbox Code Playgroud)

之后,点击事件的调用逻辑有一个小的变化:

这是新的 HTML 代码:

<a class="nav-link" (click)="openAbout()">About</a>
Run Code Online (Sandbox Code Playgroud)

这是新的 TypeScript 事件:

openAbout() {
    //Here you define the name of your component
    this.modalService.open(AboutComponent);
    //This section is if you want to have any variable to initialize
    //compConst.componentInstance.weight = undefined;
}
Run Code Online (Sandbox Code Playgroud)

另外,在您的新组件中,您需要添加更改 constructor 并添加NgbActiveModal.

constructor(public activeModal: NgbActiveModal) { }
Run Code Online (Sandbox Code Playgroud)

另一个重要的变化是关闭模态的逻辑,需要改为:

<button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
    <span aria-hidden="true">&times;</span>
</button>
Run Code Online (Sandbox Code Playgroud)

您需要将旧的:更改(click)="d('Cross click')"(click)="activeModal.dismiss('Cross click')"

有了这些变化,它就会像魅力一样发挥作用。我从这里得到了一些灵​​感:

https://stackblitz.com/edit/angular-uwtgs6