javascript - 如何访问在 ngfor 中以 Angular 2 生成的手风琴列表中的特定手风琴
问题描述
我有一个生成的子手风琴列表,每个子手风琴在父手风琴内使用 ngFor 生成的面板列表的一个面板内。我需要根据某些逻辑切换特定的子手风琴。但是它不起作用,我无法在 ngfor 生成的手风琴列表中识别出一个手风琴。
在 html 文件中
<ngb-accordion #acc1="ngbAccordion" >
<ngb-panel #parentPanel *ngFor="let parentPanel of parentPanels; let parentPanelIndex=index" id="{{parentPanelIndex}}">
<ngb-accordion #acc2="ngbAccordion" >
<ngb-panel *ngFor="let childPanel of ChildrenPanels; let childPanelIndex=index" id="{{childPanelIndex}}">
....
在 ts 文件中:
@ViewChildren('parentPanels') parentPanels: QueryList<NgbPanel>;
....
this.parentPanels.toArray()[this.parentPanelIndex]
最后一行将根据“parentPanelIndex”中存储的值给我一个特定面板的引用。现在我可以访问父面板,但我不知道如何在该面板中获取对手风琴 (acc2) 的引用。有什么建议么?
解决方案
您可以使用 querySelector 让孩子处于自定义位置,例如
const child = this.panelsView
.nativeElement
.querySelector(`:nth-child(${this.elem})`);
this.result = child.innerHTML;
你可以在这里找到一个例子https://stackblitz.com/edit/angular-dwmajp
推荐阅读
- asp.net-mvc - 避免 ASP.NET MVC5 中的重复记录
- authentication - 如何在使用 httpOnly cookie 时保护 SPA 中的私有路由
- php - 访问用户级别的 PHP 登录
- r - 为什么 '->'(12, b) 会出错?
- jenkins - Jenkins Windows 服务是否需要管理员权限?
- html - 哪种方式更好地构建图像标题?
- javascript - Firebase reauthenticateAndRetrieveDataWithCredential 问题
- jquery - 如果至少选中一个复选框,则检查长度
- asp.net - ASP.Net Core 更改默认视图文件夹位置
- node.js - 如何仅在执行功能后发送结果?