[英]how to access nested elements using ContentChildren in angular
I have the following code snippet 我有以下代码片段
<my-tabs>
<my-tab>
<tab-heading>Users</tab-heading>
<tab-content>Users Data</tab-content>
</my-tab>
<my-tab my-tab--active>
<tab-heading>Groups</tab-heading>
<tab-content>Roles Data</tab-content>
</my-tab>
</my-tabs>
I want to access all the tab-heading
so I am using @ContentChildren
decorator like this 我想访问所有的
tab-heading
所以我正在使用@ContentChildren
装饰器
@ContentChildren(TabHeading, { descendants: true })
tabHeadingList: QueryList<TabHeading>;
ngAfterViewInit() {
console.log(this.tabHeadingList);
}
but this returns the empty list. 但这将返回空列表。 Is it possible to get the same using
@ContentChildren
or any other way around? 是否可以使用
@ContentChildren
或其他方法获得相同的@ContentChildren
?
Here is TabHeading
Class 这是
TabHeading
类
@Component({
selector: "tab-heading",
template: `<li class="tab"><a href="#123456789"><ng-content></ng-content></a></li>`
})
export class TabHeading {
}
Even I tried with ngAfterContentInit
hook still not working. 即使我尝试使用
ngAfterContentInit
钩子仍然无法正常工作。
ngAfterContentInit() {
console.log("Content Init");
console.log(this.tabHeadingList);
}
I am able to get my-tab
though. 我可以得到
my-tab
。 but not able to get the nested child. 但无法得到嵌套的孩子。
TabHeading
is my custom angular component, I was not registering it to ngmodule
. TabHeading
是我的自定义角度组件,我没有将其注册到ngmodule
。
@NgModule({
imports: [CommonModule],
declarations: [MyTab, MyTabs, TabHeading],
exports: [MyTab, MyTabs, TabHeading]
})
export class TabsModule { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.