angular - 使用 ngx-bootstrap modal 将数据传递给 modal

标签 angular typescript ngx-bootstrap

我正在使用 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});
  }

最佳答案

你可以像这样使用它
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

关于angular - 使用 ngx-bootstrap modal 将数据传递给 modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62725266/

相关文章:

angular - "Expression has changed after it was checked"当子组件在 ngOnInit 上发出时

typescript - Angular2-附加变量未定义

typescript - TypeScript 中的通用类转换

ngx-bootstrap - 如何设置在 bsDaterangepicker 弹出窗口中显示的月份?

angular - 无法使用 Select 以 Angular 4 react 形式隐藏然后显示控件

angular - fromPromise 在 Observable 类型上不存在

Angular Material 找不到 Angular Material 核心主题

typescript - 运算符 '!==' 不能应用于类型 "A"| "B",但是 '===' 可以吗?

angular - Valor-software/ngx-bootstrap 将组件放入动态创建的内容中

ngx-bootstrap - 手动触发预输入