【发布时间】:2020-11-08 23:18:00
【问题描述】:
我正在尝试从父组件打开 ng2-bootstrap 模式。我创建了一个 ModalComponent 并且我“exportAs:child”。我的 ModalComponent 看起来像
import { Component, Input, ViewChild } from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/ng2-bootstrap';
@Component({
selector: 'modal',
moduleId: module.id,
templateUrl: 'modal.component.html',
styleUrls: ['modal.component.css'],
exportAs: 'child'
})
export class ModalComponent {
@ViewChild('childModal') public childModal:ModalDirective;
public show( variant ):void {
console.log( this.childModal );
this.childModal.show();
}
public hide():void {
this.childModal.hide();
}
}
我在父模板中包含模态
<modal #c="child"></modal>
... 并从父模板调用它为
<button type="button" class="btn btn-primary" (click)="c.show(variant)">open</button>
我正确地点击了 ModalComponent 内的“show”方法,但“childModal”的值始终未定义(Nb:console.log() 语句)。
非常感谢任何指针。
【问题讨论】:
-
您能发布您的
modal.component.html文件吗?并检查这个 plunker plnkr.co/edit/951NyF20qXudDtZo5OAx?p=preview -
谢谢!我检查了你的 plunker,发现我的错误出在 HTML 中,正如你所怀疑的那样。我会在下面写出解决方案。
-
@yurzui 只是要注意,截至今天 plunker 无法正常工作,显然是由于 ng2-bootstrap 从其
<script>标签加载的方式。 This fork 带有不同的<script>标签似乎可以工作。 HTH
标签: angular ng2-bootstrap