有没有机会获得ng-boostrap弹出模式作为组件,并将从另一个组件获取

时间:2018-12-31 08:56:11

标签: angular bootstrap-modal angular-components ng-bootstrap

我在应用程序中使用 this modal ,我有两个组件。

父HTML:

<div id="list">
<ul><li class"list li><a id="modal1" (click) = "openmodal($event)"></a>
....
....
</div>

父组件:

import { Component, OnInit } from '@angular/core';
import { ChildModalComponent } from 'src/app/popup-modal/popup-modal.component';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'parentcomp',
  templateUrl: './parentcomp.html',
  styleUrls: ['./parentcomp.component.scss']
})
export class ParentComponent implements OnInit {
  private loadComponent = false;
  closeResult: string;
  constructor(private modalService : NgbModal) {}
  ngOnInit() {
  }
  openmodal(event) {
    alert("clicked!")
    //this.modalService = modalService;
    let component = new ChildModalComponent(modalService);
    //component.open("Hello");
    //this.modalService.open("Hello!!");
  }
}

当我打开this.modalService.open("Hello!!")时-没有ng-template的打开的普通文本弹出窗口

儿童HTML:

<ng-template #content let-modal>
    <div class="modal-header">
      <h4 class="modal-title" id="modal-basic-title">Profile update</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">
      <form>
        <div class="form-group">
          <label for="dateOfBirth">Date of birth</label>
          <div class="input-group">
            <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
            <div class="input-group-append">
              <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
            </div>
          </div>
        </div>
      </form>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
    </div>
  </ng-template>

子组件:

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

@Component({
  selector: 'ngbd-modal-basic',
  templateUrl: './child.component.html'
})
export class ChildModalComponent {
  closeResult: string;    
  constructor(private modalService: NgbModal) {}

  open(content) {
    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return  `with: ${reason}`;
    }
  }
}

如何打开child.component.html弹出窗口?以及如何在点击openmodal($event)时传递ID或标题?

输出屏幕:this.serviceModal.open("Hello") 代码:Editor Code

1 个答案:

答案 0 :(得分:0)

您的父组件包含ng-template,它包装了不需要了解模态的子组件。

子组件可以具有@Input()@Output()绑定,以将数据传递给子组件或从子组件内部与父组件进行交互(即关闭模式)。

粗略的例子:

父模板

<ng-template #content let-modal let-inputData="inputData">
    <child-component
        [input]="inputData"
        (failed)="modal.dismiss()"
        (succeeded)="modal.dimiss()"
    />
</ng-template>

<button (click)="modalOpen(content)">Launch modal</button>

家长班

....
modalOpen(content) {
    this.modalService.open(content)...
}

子组件不需要实现任何特定于模式的功能。

[更新]

如注释中所述,该特定示例不起作用,因为该组件不是模块entryComponents的一部分。此外,子组件没有编程的功能,因为子组件仅在其模板中定义了ngtemplate而不对其进行任何处理。