Rue*_*hri 22 ng-bootstrap angular
我在Angular 4中使用ng-bootstrap,具体用例是"Component as content"的模式(来自https://ng-bootstrap.github.io/#/components/modal/examples的第二个场景).
我想从孩子那里向父母发出一些数据.为此,我从示例中创建了一个非工作插件:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
modal.component.html
<button class="btn btn-lg btn-outline-primary" (click)="open()">Launch demo modal</button>
Run Code Online (Sandbox Code Playgroud)
如您所见,我只将EventEmitter添加到NgbdModalContent组件中.我想要的是NgbdModalComponent接收testclick事件并将其记录到控制台.我在哪里可以把它放在上面的代码中?
我知道这里有一个非常类似的问题从引导模态到父模式的事件发射器,但我认为它仍然是一个非常不同的实现方案,因为我在这里直接打开模态作为组件.
显然我更喜欢一些简单的解决方案,我不需要为modalService自己进入代码......
pko*_*rce 46
它实际上非常简单,因为您可以直接订阅@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);
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个工作的插件:http://plnkr.co/edit/rMJ7qfSSLK0oHspIJbDB?p = preview
旁注:我不确定你的确切用例是什么,但是从bootstrap modal到parent的Event发射器的答案显示了模态开启器和用作模态内容的组件之间的首选通信方法.
| 归档时间: |
|
| 查看次数: |
10811 次 |
| 最近记录: |