[英]Angular2: get the count of child components - when in the lifecycle and how
So, for illustration purposes lets say I have two components因此,出于说明目的,假设我有两个组件
where tabset is the parent that has many tabitems其中 tabset 是具有许多 tabitems 的父项
So, depending on how many tabitems there are inside tabset I will calculate some stuff...所以,根据 tabset 中有多少 tabitem 我会计算一些东西......
so, how do I get the item count?那么,我如何获得项目计数?
(the item count should be accessible in the child component)... (项目计数应该可以在子组件中访问)...
so I have reference of the parent like this所以我有这样的父母的参考
<tabset #tabset>
<tabitem [tabset]="tabset">....</tabitem>
<tabitem [tabset]="tabset">....</tabitem>
<tabitem [tabset]="tabset">....</tabitem>
</tabset>
So how do I go from here?那我怎么走呢? When in the life-cycle of tabset I will know how many elements there are actually in?在 tabset 的生命周期中,我会知道实际上有多少个元素? and do I need to use vanilla javascript with ElementRef and getElementsByTagName ?我是否需要使用带有ElementRef和getElementsByTagName 的vanilla javascript? Or is there any other, more angular friendly way?或者还有其他更友好的方式吗?
One extra question.... how can I pass the component's reference via ng-content ?一个额外的问题......我如何通过ng-content传递组件的引用?
tabset.html tabset.html
<div class="...." #tabset>
<ng-content></ng-content>
@ContentChildren(TabItem) tabItems: QueryList<TabItem>;
ngAfterContentInit() {
console.log(this.tabItems.length);
}
Note: TabItem
can be a component too, not only a directive注意: TabItem
也可以是一个组件,而不仅仅是一个指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.