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

angular – @ViewChildren(TemplateRef)未定义元素

发布时间:2020-12-17 10:24:41 所属栏目:安全 来源:网络整理
导读:如何在组件中获取所有模板(TemplateRef)的集合?它适用于ViewChild,但ViewChildren未定义… 我使用了这个question. Plunker的解决方案和完整的例子. @Component({ selector: 'my-app',template: ` div template #model.../template template #color.../templ
如何在组件中获取所有模板(TemplateRef)的集合?它适用于ViewChild,但ViewChildren未定义…

我使用了这个question. Plunker的解决方案和完整的例子.

@Component({
    selector: 'my-app',template: `
            <div>
              <template #model>...</template>
              <template #color>...</template>
            </div>`
  })
  export class App {
  @ViewChild('model') model;
  @ViewChild('color') color;
  @ViewChildren(TemplateRef) templates: QueryList<TemplateRef>;

  ngAfterContentInit() {
    console.log(this.templates); // undefined
    console.log(this.model); // TemplateRef_ {...}
  }
}

我需要一个网格组件的模板,可以为列定义模板.不幸的是ng-content doesn’t support动态投影,所以我试图用模板实现它.

如果你移动console.log(‘child’,this.templates);到ngAfterViewInit()然后它的工作原理.
ngAfterViewInit() {
  console.log('child',this.templates);
}

我原本以为它也适用于ngAfterContentInit().在Angular版本中,ngAfterContentInit()似乎在ngAfterViewInit()之前运行.我确信这是早先的另一种方式.

Plunker example

(编辑:李大同)

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

    推荐文章
      热点阅读