如何在 ng-template 上的指令中使用 contentChildren?
如何在 ng-template 上的指令中使用 contentChildren?
结论
放在 <ng-template> 上的指令,无法通过 contentChildren(TestchildComponent) 查询到模板内部实例化出来的 TestchildComponent。contentChildren 只在其视图作用域内查询;<ng-template> 上的指令属于声明该模板的父视图,而模板内容要等到在 ngTemplateOutlet 等位置实例化后,才进入另一个嵌入视图。两者不在同一个查询作用域,因此这里拿到的是 Signal([])。
如果目的是通过 ng-template 的 context 给 TestchildComponent 传数据,同时还要在某个指令里查询子组件,可以把负责查询的指令从 <ng-template> 移到模板内部的实际宿主元素上,比如 div 或自定义容器组件。ng-template 只保留提供 TemplateRef 和 context 的职责。
原因
Angular 的内容查询不会跨任意视图边界递归查找 DOM 节点。<ng-template> 只是模板声明,里面的内容默认不会创建组件实例;用 *ngTemplateOutlet 渲染它时,TestchildComponent 是在 outlet 所在的嵌入视图里创建的。TestparentDirective 放在 <ng-template app-testparent> 上时,实例仍关联于声明该模板的视图,不会随模板内容进入嵌入视图,所以 contentChildren 看不到 TestchildComponent。
把 <ng-template> 换成 div 后查询能工作,是因为这时 TestparentDirective 和 TestchildComponent 处在同一个视图的父子结构中。
解决步骤
- 保留一个
ng-template,用来接收 context 并传给子组件。 - 不要在
ng-template上做contentChildren查询;把需要查询子组件的指令放到模板内部一个真实元素上。 - 如果组件还需要拿到这个模板,可以用模板引用变量配合
@ContentChild('tpl', { read: TemplateRef }),或者在合适场景下使用contentChild信号查询。 - 在
ngTemplateOutlet中通过context: { $implicit: ... }传值,模板里用let-data接收,再绑定给TestchildComponent。
代码示例
App component template:
<app-test>
<ng-template #childTpl let-data>
<div app-testparent>
asdf
<app-testchild [data]="data"></app-testchild>
</div>
</ng-template>
</app-test>
TestComponent:
import { Component, ContentChild, TemplateRef } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-test',
standalone: true,
imports: [CommonModule, TestparentDirective, TestchildComponent],
template: `
<ng-container
*ngTemplateOutlet="childTpl ?? null; context: { $implicit: 1 }">
</ng-container>
<ng-container
*ngTemplateOutlet="childTpl ?? null; context: { $implicit: 2 }">
</ng-container>
`
})
export class TestComponent {
@ContentChild('childTpl', { read: TemplateRef })
childTpl?: TemplateRef<{ $implicit: number }>;
}
如果使用信号查询,可以写成:
childTpl = contentChild('childTpl', { read: TemplateRef });
TestparentDirective:
import { Directive, contentChildren } from '@angular/core';
@Directive({
selector: '[app-testparent]',
standalone: true
})
export class TestparentDirective {
children = contentChildren(TestchildComponent);
}
TestchildComponent:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-testchild',
standalone: true,
imports: [],
template: '<p>testchild works! data: {{data}}</p>'
})
export class TestchildComponent {
@Input() data!: number;
}
这样,每次 ngTemplateOutlet 创建嵌入视图时,app-testparent 和 app-testchild 都在同一个嵌入视图里,contentChildren 才能查到当前模板实例中的子组件。注意上面的 childTpl 会被渲染两次,因此会创建两个 TestchildComponent 实例,分别对应 context 中的 1 和 2。
注意事项
- 不要把
contentChildren和viewChildren混用。contentChildren查的是内容投影/内容查询作用域;viewChildren查的是组件自身模板视图里的元素。这里的问题在于ng-template上的指令和模板内容不在同一个视图作用域。 - 如果必须保留
<ng-template app-testparent>并让该指令感知子组件,通常只能改设计:例如让渲染模板的组件使用viewChildren观察实例化后的子组件,再通过服务、事件或输入把结果传回指令;这会把职责移到渲染方,而不是在<ng-template>指令里直接查。 - 读取信号查询结果时要注意生命周期时机。不要在 constructor 里就假设已经有子组件结果;需要时在
ngAfterContentInit、ngAfterViewInit或通过effect/computed读取,具体取决于查询类型和实例化时机。 - 使用
@ContentChild('childTpl', { read: TemplateRef })时,模板引用变量必须写在投影进来的<ng-template>上;如果结构变化,查询定位符也要同步调整。
备注:内容仅供参考。
