小编Jör*_*zen的帖子

为什么@ContentChildren在查询匹配的选择器时包含self(this)?

我有一个可以嵌套的组件,它试图查询其子组件.

@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

6
推荐指数
1
解决办法
518
查看次数

标签 统计

angular ×1