加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 使用ng-content在父项呈现项目时未填充@ContentChild

发布时间:2020-12-17 17:41:47 所属栏目:安全 来源:网络整理
导读:我有三个组件:Panel,PanelGroup(ul)和PanelItem(li): 面板: @Component({selector: "panel",directives: [PanelGroup,PanelItem],template: ` panel-group ng-content select="panel-item"/ng-content /panel-group`})export default class Panel {} pane
我有三个组件:Panel,PanelGroup(ul)和PanelItem(li):

面板:

@Component({
selector: "panel",directives: [PanelGroup,PanelItem],template: `
  <panel-group>
    <ng-content select="panel-item"></ng-content>
  </panel-group>
`})

export default class Panel {}

panelGroup中:

@Component({
selector: "panel-group",directives: [forwardRef(() => PanelItem)],template: `
  <ul>
    <ng-content></ng-content>
  </ul>`
})

export default class PanelGroup {
  @ContentChildren(forwardRef(() => PanelItem)) items;

  //I need to access children here and modify them eventually:
  ngAfterContentInit() {
    console.log(this.items.toArray()); //the array is always empty
  }
}

PanelItem:

@Component({
selector: "panel-item",directives: [forwardRef(() => PanelGroup)],template: `
  <li>
    <span (click)="onClick()">
        {{title}}
    </span>
    <panel-group>
        <ng-content select="panel-item"></ng-content>
    </panel-group>
  </li>`
})

export default class PanelItem {
  @Input() title = 'SomeTitle';
}

如上例所示,我尝试在PanelGroup组件中获取内容子项,但集合始终为空.还尝试将选择器添加到其中的“ng-content” – 在这种情况下,子节点永远不会被渲染,这有点奇怪.

我错过了什么吗?

这是插件:

> demo

解决方法

使用后代:true:

@ContentChildren(forwardRef(() => PanelItem),{descendants: true}) items;

另见angular 2 / typescript : get hold of an element in the template

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读