【问题标题】:How I can get reference of same type of child component inside parent component如何在父组件中获取相同类型子组件的引用
【发布时间】:2017-02-09 06:50:29
【问题描述】:

为了获取子组件引用,我们有选项-

@ViewChild(ChildComponent)
childComp: ChildComponent;

我想知道如果我有两次或更多次相同的子组件怎么办,如何在不同的不同变量中获取所有相同类型的子组件。例如,在我的父模板中,我有-

<div>
  <child-selector></child-selector>
  <child-selector></child-selector>
</div>

如何在父组件中获取相同类型子组件的引用?任何帮助将不胜感激

【问题讨论】:

    标签: angular


    【解决方案1】:

    使用 ViewChildren

    @ViewChildren(ChildComponent)
    childComps: QueryList<ChildComponent>;
    

    *位于组件模板内的子元素称为*view children*。另一方面,**元素 在主机的开始标签和结束标签之间使用 给定组件的元素称为 *content children **。

    http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/

    【讨论】:

    猜你喜欢
    • 2019-05-19
    • 2020-02-12
    • 1970-01-01
    • 2017-05-25
    • 2016-07-01
    • 2016-06-12
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多