我在 Angular 2 中使用 ng-bootstrap。我无法使用 @ViewChild
从我的组件访问 #tabs
变量。仅当我在模态指令内使用 tab 指令时才会发生这种情况。这是我的代码:
auth.component.html
<template #modal let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title"></h4>
<button type="button" class="close" aria-label="Close" (click)="close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<ngb-tabset #tabs="ngbTabset" (tabChange)="changeTabs($event)">
<ngb-tab title="Login" id="login">
<template ngbTabContent>
...
</template>
</ngb-tab>
<ngb-tab title="Sign up" id="signUp">
<template ngbTabContent>
...
</template>
</ngb-tab>
</ngb-tabset>
</div>
</template>
auth.component.ts
import {Component, ViewChild} from '@angular/core';
import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
@Component({
moduleId: module.id,
selector: 'st-auth',
templateUrl: 'auth.component.html'
})
export class AuthComponent {
/*
* ------------ FIELDS -----------------
*/
@ViewChild('modal') modal;
private modalElement:any;
@ViewChild('tabs') tabs;
private tabsElement:any;
/*
* ------------ INITIALIZE -----------------
*/
ngAfterViewInit() {
this.modalElement = this.modal;
this.tabsElement = this.tabs;
console.log(this.tabs); //show 'undefined'
}
constructor(private modalService: NgbModal) {}
}
最佳答案
我也遇到了同样的问题。当使用 元素时,Angular 不会将其视为普通 DOM 元素,因此您无法通过 ViewChild 装饰器访问它。我最终做的是创建一个包含对话框内容的新组件,并将该组件传递给 modalService.open()。例子可见here .
提示:不要忘记将模态组件添加到模块的 entryComponents 数组中
关于javascript - 访问 ng-bootstrap 中模态框内的选项卡元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927993/