如何在 Angular 7 中将数据从组件传递到 ngBootstrap 模态?

How to pass data from component to ngBootstrap modal in Angular 7?

我有一个 table 使用 ngFor 显示数据。我需要在点击时以模式显示单个行数据。我可以通过将行作为单击参数传递但无法将其传递到模态中来在控制台中记录它。

假设 table 数据是从服务中获取的,模态是模板引用,它与 table.

在同一组件文件中

这是一个 fiddle 创建的 https://stackblitz.com/edit/angular-xr8ud5

我用过angular1.x,数据是通过resolve传递过来的。我是 Angular 的新手,从未在 ngBootstrap 中工作过。

感谢任何帮助。谢谢

您可以在组件中为模态数据创建变量: StackBlitz

或者您可以为模态创建组件:
模态-basic.ts

openModal(dataToModal) {
        const modalRef = this.modalService.open(ModalComponent);
        modalRef.componentInstance.inputDataInModalComponent = dataToModal;
        modalRef.result.then(() => {});
        }, () => {});
    }

modal.component.ts

export ModalComponent implements OnInit{
    @Input() inputDataInModalComponent: any;

    ngOnInit() {
         console.log(inputDataInModalComponent);
    }
}

只需要将选定的行存储在一个变量中,然后我们就可以在 HTML

上显示该变量

在您的 stackblitz 中,将 modal-basic.html 更改为:

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let tableRow of table" (click)="open(content, tableRow)">
      <th scope="row">{{tableRow.id}}</th>
      <td>{{tableRow.first}}</td>
      <td>{{tableRow.last}}</td>
      <td>{{tableRow.handle}}</td>
    </tr>
  </tbody>
</table>


<ng-template #content let-modal>
  <div class="modal-header">
    <h4 class="modal-title" id="modal-basic-title">Details</h4>
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    id:<u>{{selectedRow.id}}</u><br>
    first:<u>{{selectedRow.first}}</u><br>
    last:<u>{{selectedRow.last}}</u><br>
    handle:<u>{{selectedRow.handle}}</u>
  </div>
</ng-template>
<hr>

<pre>{{closeResult}}</pre>

在您的 stackblitz 中,将 modal-basic.ts 更改为:

import {Component} from '@angular/core';

import {NgbModal} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'ngbd-modal-basic',
  templateUrl: './modal-basic.html'
})
export class NgbdModalBasic {
  closeResult: string;
  table = [
  {
    "id": 1,
    "first":"Mark",
    "last": "Otto",
    "handle": "@mdo"
  },{
    "id": 2,
    "first":"Jacob",
    "last": "Thornton",
    "handle": "@fat"
  },{
    "id": 3,
    "first":"Larry",
    "last": "the Bird",
    "handle": "@twitter"
  }
];

selectedRow;

  constructor(private modalService: NgbModal) {}

  open(content, tableRow) {
    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'});
    //console.log(tableRow)
    this.selectedRow = {id:tableRow.id, first: tableRow.first, last:tableRow.last, handle:tableRow.handle};
  }
}

可以通过 Angular 的 2-way binding 将数据从您的组件传递到 ngBoostrap 模态。这实际上与您在 Angular 1.x!

上执行双向绑定的方式完全相同

我为你做了一个demo

首先,在您的模型上-basic.ts,您为模态定义模型。然后,您为 modalContent 模型 属性 分配 table 行数据。

modalContent:undefined
  .
  .

open(content, tableRow) {
  this.modalContent = tableRow
  this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'});
}

在您的模态 basic.html 上,您可以使用 {{ ... }} template expression 在模态本身上显示模型的各个属性。

<ng-template #content let-modal>
  <div class="modal-header">
    <h4 class="modal-title" id="modal-basic-title">Details</h4>
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    id: {{ modalContent.id }}
    <br>
    first: {{ modalContent.first }}
    <br>
    last: {{ modalContent.last }}
    <br>
    handle: {{ modalContent.handle }}
  </div>
</ng-template>