Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does @ContentChildren include self (this) when querying a matching selector?

Tags:

angular

I have a component that can be nested and it tries to query its children.

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

However, the QueryList does not just include all child-components, but also the querying component itself (== this).

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

The output is [3][1][1] rather than [2][0][0].

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

Can this be prevented? For DI there is @SkipSelf, but it doesn't seem to apply with @ContentChildren.

like image 399
Jörg Frantzen Avatar asked Nov 17 '16 16:11

Jörg Frantzen


1 Answers

There is an open bug to get this changed and it was confirmed. This will change in the future.

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

like image 200
Günter Zöchbauer Avatar answered Nov 15 '22 11:11

Günter Zöchbauer