【问题标题】:How to pass data from component to ngBootstrap modal in Angular 7?如何在 Angular 7 中将数据从组件传递到 ngBootstrap 模式?
【发布时间】:2019-08-11 22:11:47
【问题描述】:

我有一个使用 ngFor 显示数据的表格。我需要在单击时以模式显示单个行数据。我可以通过将行作为单击参数传递来将其登录到控制台,但无法在模式内传递它。

假设表格数据是从服务中获取的,并且模态是一个模板引用,它与表格在同一个组件文件中。

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

我使用了 angular 1.x 并且数据是通过解析传递的。我是 Angular 新手,从未在 ngBootstrap 工作过。

感谢任何帮助。谢谢

【问题讨论】:

标签: javascript angular angular7 ng-bootstrap


【解决方案1】:

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

或者您可以为模态创建组件:
modal-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);
    }
}

【讨论】:

    【解决方案2】:

    只需将选定的行存储在一个变量中,然后我们就可以在 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};
      }
    }
    

    【讨论】:

      【解决方案3】:

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

      我为你做了一个demo

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

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

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

      <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>
      

      【讨论】:

      • 非常感谢!现在可以了。我只需要将传递的参数保存在 .open() 的变量中就这么简单
      • 干杯 :) 很高兴为您提供帮助!
      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 2020-04-05
      • 2017-06-25
      • 2020-05-26
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多