首页 > 解决方案 > 使用模型向组件发送数据

问题描述

嗨,我正在使用带有 ng-boostrap 的 Angular 6。如何在打开 PopUP 时向组件发送数据?这实际上是在弹出窗口中弹出。

调用堆叠模型

https://ng-bootstrap.github.io/#/components/modal/examples

popUPOrders() {
this.modalService.open(OrdersComponent, {
  size: 'lg'
});

打开 POPUP 的唯一方法是使用 modelSerivce.open 并且没有附加数据的字段。

标签: angular6ng-bootstrap

解决方案


要传递的数据需要是内容组件上的输入属性。在您的情况下,为 OrdersComponent 组件添加输入属性。
链接: https
: //ng-bootstrap.github.io/#/components/modal/examples Bootstrap 站点示例(组件作为内容):

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

@Component({
  selector: 'ngbd-modal-content',
  template: `
    <div class="modal-header">
      <h4 class="modal-title">Hi there!</h4>
      <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Hello, {{name}}!</p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
    </div>
  `
})
export class NgbdModalContent {
  @Input() name;

  constructor(public activeModal: NgbActiveModal) {}
}

@Component({
  selector: 'ngbd-modal-component',
  templateUrl: './modal-component.html'
})
export class NgbdModalComponent {
  constructor(private modalService: NgbModal) {}

  open() {
    const modalRef = this.modalService.open(NgbdModalContent);
    modalRef.componentInstance.name = 'World';
  }
}

推荐阅读