angular - 如何将参数传递给模态?

标签 angular typescript modal-dialog ng-bootstrap

这就是我使用 ng2-bootstrap 模态的方式:

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

@Component({
  selector: 'add-customer-modal',
  template: `
    <template #test let-c="close" let-d="dismiss">
      <div class="modal-header">
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
      </div>
    </template>
    <button type="button" class="btn btn-primary btn-sm" (click)="open(test)"><i class="fa fa-plus"></i> <i class="fa fa-user-o"></i></button>
  `
})
export class AddCustomerModal {

  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content, { size: 'lg' }).result.then((result) => {
      console.log(result);
    }, (reason) => {
      console.log(reason);
    });
  }
}

我有点困惑,因为我认为内容是用来将参数传递给模态的。但在我看来,打开方法只需要名称即可找到正确的模板?

那么如何传递参数呢?

最佳答案

任何仍在绊倒这个的人可能会发现这很方便,您需要做的就是在您的 ModalComponent 中声明一个字段,如下所示:

 modalHeader: string;
 advertiser: Advertiser;

您可以在打开模式时通过执行以下操作来设置这些字段。

advertiserModalShow() {
    const activeModal = this.modalService.open(AdvertiserModal, { size: 'lg' });
    activeModal.componentInstance.modalHeader = 'Advertiser';
    activeModal.componentInstance.advertiser = this.selectedAdvertiser;
}

在您的模板中,您可以像这样访问它们:

value={{advertiser.code}}

希望这对您有所帮助。

关于angular - 如何将参数传递给模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139842/

相关文章:

angular - mat-autocomplete 在输入焦点上自动隐藏占位符文本

angular - 将Latest合并为2个独立的json对象

javascript - Angular2 对异步/等待的 promise

reactjs - react 上下文 : Provide instance of an Api Manager

angular - 如何在 Angular 7 中从 ckeditor 5 检索数据?

Angular Material : mat-select not selecting default

typescript - Facebook Workplace 中的 BotFramework 集成

javascript - 如果文本区域有输入,如何防止关闭 blockUI

html - Bootstrap 模式错误定位

jQuery 模态对话框与 html() 冲突并返回 false;