使用 ngx-bootstrap modal 将数据传递到 modal

问题描述 投票:0回答:2

我正在使用 Angular 8 和 ngx-boostrap 打开模态并将数据从父级传递到模态。但没有按预期工作。我应该怎么做才能让它工作?..这是我的 stackblitz 演示和代码

HTML

<button type="button" class="btn btn-primary" (click)="openModal(template)">Open modal</button>
 
<ng-template #template>
  <div class="modal-header">
    <h4 class="modal-title pull-left">Modal</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    Just a modal with a {{initialState.data1}}
  </div>
</ng-template>

组件

openModal(template: TemplateRef<any>) {
    const initialState = {
    data1 : 'foo'
}
    this.modalRef = this.modalService.show(template, {initialState});
  }
angular typescript ngx-bootstrap
2个回答
5
投票

你可以这样使用它

HTML

<ng-template #template>
  <div class="modal-header">
    <h4 class="modal-title pull-left">Modal for data : {{ modalService.config.initialState.data1 }}</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    This is a modal.
  </div>
</ng-template>

组件

openModal(template: TemplateRef<any>) {
    const user = {
        data1 : 'foo'
      };
    this.modalRef = this.modalService.show(template, {
      initialState : user
    });
  }

这个在线DEMO


0
投票

我可以使用以下方法使其工作:

<ng-template #template>
  <div class="modal-header">
  <h4 class="modal-title pull-left">Modal for data : {{ data1 }}</h4>
  <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
    <span aria-hidden="true">&times;</span>
  </button>
  </div>
  <div class="modal-body">
    This is a modal.
  </div>
</ng-template>

和:

openModal(template: TemplateRef<any>) {
  const initialState = {
    data1 : 'foo'
  };
  this.modalRef = this.modalService.show(template, {initialState});
}

非常感谢:https://levelup.gitconnected.com/how-to-pass-data- Between-ngx-bootstrap-modal-and-parent-e348cd596cf7

© www.soinside.com 2019 - 2024. All rights reserved.