【发布时间】:2017-12-30 15:15:47
【问题描述】:
我在 Angular 4 中使用 ng-bootstrap,具体用例是“组件作为内容”的模式(来自https://ng-bootstrap.github.io/#/components/modal/examples 的第二个场景)。
我想从子级向父级发送一些数据。为此,我从示例中创建了一个无效的 plunk:
modal.component.ts
import {Component, Input, Output, EventEmitter} from '@angular/core';
import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ngbd-modal-content',
template: `
<div class="modal-header">
<h4 class="modal-title">Hi there!</h4>
</div>
<div class="modal-body">
<p>Hello!</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="testclick('teststring')">Testclick</button>
</div>
`
})
export class NgbdModalContent {
@Output() clickevent = new EventEmitter<string>();
constructor(public activeModal: NgbActiveModal) {}
testclick(teststring: string) {
this.clickevent.emit(teststring);
}
}
@Component({
selector: 'ngbd-modal-component',
templateUrl: 'src/modal-component.html'
})
export class NgbdModalComponent {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(NgbdModalContent);
}
}
modal.component.html
<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>
如您所见,我只是将 EventEmitter 添加到 NgbdModalContent 组件中。我想要的是 NgbdModalComponent 接收testclick 事件并将其记录到控制台。我在上面的代码中可以放在哪里?
我知道这里有一个非常相似的问题Event emitter from bootstrap modal to parent,但我认为这仍然是一个非常不同的实现方案,因为我在这里直接将模态作为组件打开。
显然我更喜欢一些简单的解决方案,我不必进入 modalService 本身的代码...
【问题讨论】:
标签: angular ng-bootstrap