【问题标题】:Get child component inside of a @ChildComponent Angular在 @ChildComponent Angular 中获取子组件
【发布时间】:2017-11-13 19:16:34
【问题描述】:

我正在尝试获取另一个组件的子组件,这可能吗?例如,我有这个 QueryList:

@ContentChildren(SysColumn) syscolumns: QueryList<SysColumn>;

它将创建一个包含所有实例化的 SysColumns 类的 QueryList,这很完美,但我需要获取 SysColums 中子组件的列表... HTML 将是这样的:

<parent>    
 <sys-column>
  <child></child>
 </sys-column>
 <sys-column>
   <child></child>
 </sys-column>
</parent>

如果我对 QueryList 执行 Foreach,它将显示两个 SysColms,但现在我需要拥有内部组件。我怎样才能实现它

【问题讨论】:

  • 我已经在父组件中声明了它,我需要有类似 ContentChildren 的东西,我可以通过父组件访问

标签: javascript angular typescript components children


【解决方案1】:

如果要查询子组件,@ContentChildren()需要长这样

@ContentChildren(Child, {descendants: true}) children: QueryList<Child>;

{descendants: true} 可能是默认值(不记得了)

【讨论】:

【解决方案2】:

我在 Angular GitHub 上找到了解决方案:https://github.com/angular/angular/issues/11039#issuecomment-242091301

【讨论】:

    猜你喜欢
    • 2019-03-10
    • 2019-01-03
    • 2018-02-27
    • 1970-01-01
    • 2016-07-15
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多