0
内相同类型的子组件的参考为了得到孩子组件参考我们有选项 -我怎样才能得到父组件
@ViewChild(ChildComponent)
childComp: ChildComponent;
我不知道如果我有相同的子组件的两倍以上,怎么可以在不同的变量中获取所有相同类型的子组件。例如,在我的父模板中,我有 -
<div>
<child-selector></child-selector>
<child-selector></child-selector>
</div>
我怎样才能获得父组件内同一类型的子组件的引用?任何帮助将高度赞赏
日Thnx的答案,当我试图让使用childComps.length它给未定义childComps的长度是多少?我如何迭代并获取每个子选择器引用? – deen
你需要挂钩到'AfterViewInit'生命周期,在这个事件触发后,你可以遍历你的列表。 https://angular.io/docs/ts/latest/api/core/index/AfterViewInit-class.html,https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html –