【问题标题】:Why does @ContentChildren include self (this) when querying a matching selector?为什么@ContentChildren 在查询匹配选择器时包含 self (this)?
【发布时间】:2017-04-01 07:14:42
【问题描述】:

我有一个可以嵌套的组件,它会尝试查询其子级。

@Component({
    selector: "container",
    template: `[{{this.children.length}}]<ng-content></ng-content>`
})
export class ContainerComponent {
    @ContentChildren(ContainerComponent) public children:QueryList<ContainerComponent>;
}

但是,QueryList 不仅包括所有子组件,还包括查询组件本身 (== this)。

<container>
    <container></container>
    <container></container>
</container>

输出是 [3][1][1] 而不是 [2][0][0]。

https://plnkr.co/edit/mGuJEE60QUCXYb3jIYUx?p=preview

这可以预防吗?对于 DI,有 @SkipSelf,但它似乎不适用于 @ContentChildren。

【问题讨论】:

    标签: angular


    【解决方案1】:

    有一个未解决的错误可以对此进行更改,并且已得到确认。 这将在未来发生变化。

    https://github.com/angular/angular/issues/10098#issuecomment-235157642

    【讨论】:

    • 2年后,Angular 7.0.2,bug依然存在。
    猜你喜欢
    • 2013-03-11
    • 2013-04-04
    • 2011-03-17
    • 1970-01-01
    • 2013-07-17
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多