我有一个可以嵌套的组件,它试图查询其子组件.
@Component({
selector: "container",
template: `[{{this.children.length}}]<ng-content></ng-content>`
})
export class ContainerComponent {
@ContentChildren(ContainerComponent) public children:QueryList<ContainerComponent>;
}
Run Code Online (Sandbox Code Playgroud)
但是,QueryList不仅包括所有子组件,还包括查询组件本身(== this).
<container>
<container></container>
<container></container>
</container>
Run Code Online (Sandbox Code Playgroud)
输出为[3] [1] [1]而不是[2] [0] [0].
https://plnkr.co/edit/mGuJEE60QUCXYb3jIYUx?p=preview
这可以预防吗?对于DI,有@SkipSelf,但它似乎不适用于@ContentChildren.
angular ×1