EventEmitter 从 bootstrap 模态组件到 parent
EventEmitter from bootstrap modal component to parent
我正在使用 ng-bootstrap 和 Angular 4,具体用例是带有“组件作为内容”的模式(来自 https://ng-bootstrap.github.io/#/components/modal/examples 的第二个场景)。
我想从 child 发送一些数据到 parent。为此,我从示例中创建了一个 non-working 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
事件并将其记录到控制台。我可以把它放在上面的代码什么地方?
我知道这里有一个非常相似的问题,但我认为它仍然是一个非常不同的实现方案,因为我在这里直接将模态作为组件打开。
显然我更喜欢一些简单的解决方案,我不必进入 modalService
本身的代码...
其实非常简单,你可以直接订阅作为模态内容打开的组件的@Output
事件:
export class NgbdModalComponent {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.name = 'World';
modalRef.componentInstance.clickevent.subscribe(($e) => {
console.log($e);
})
}
}
这是一个正在工作的插件:http://plnkr.co/edit/rMJ7qfSSLK0oHspIJbDB?p=preview
旁注:我不确定您的 确切 用例是什么,但 的答案显示了模态开启器和所用组件之间的首选通信方法作为模态的内容。
我正在使用 ng-bootstrap 和 Angular 4,具体用例是带有“组件作为内容”的模式(来自 https://ng-bootstrap.github.io/#/components/modal/examples 的第二个场景)。
我想从 child 发送一些数据到 parent。为此,我从示例中创建了一个 non-working 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
事件并将其记录到控制台。我可以把它放在上面的代码什么地方?
我知道这里有一个非常相似的问题
显然我更喜欢一些简单的解决方案,我不必进入 modalService
本身的代码...
其实非常简单,你可以直接订阅作为模态内容打开的组件的@Output
事件:
export class NgbdModalComponent {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.name = 'World';
modalRef.componentInstance.clickevent.subscribe(($e) => {
console.log($e);
})
}
}
这是一个正在工作的插件:http://plnkr.co/edit/rMJ7qfSSLK0oHspIJbDB?p=preview
旁注:我不确定您的 确切 用例是什么,但