I have misunderstood @ContentChildren. It does not represent all content. It represents @Components in ng-content.
To directly answer the question:
it is still unclear how i would acquire @Components inside ng-content if i don't know their types ahead of time. But i realise now that was a bit silly: if i don't know what they are, what exactly do i think i'm going to do with them?
To indirectly answer the question:
to access all the contents of ng-content i can introduce a template variable (#items) into the parent of the ng-content and reference that via @ViewChild as an elementRef. Then the children can be counted this way:
@Component({
selector: 'my-container',
template: `
<div class="my-container">
<div class="my-container-nav"><a class="my-container-previous"></a></div>
<div class="my-container-body">
<div #items class="my-container-items">
<ng-content></ng-content>
</div>
</div>
<div class="my-container-nav"><a class="my-container-next"></a></div>
</div>
`
})
export class MyContainer implements OnInit {
@ViewChild('items') items: ElementRef;
ngOnInit() {
console.log('count is: ' + this.items.nativeElement.children.length);
}
}
For others facing similar confusion, i can expand on @ContentChildren a bit more. Say i have 2 @Components: component-a and component-b and i know that consumer will place these inside the content of my component:
<my-component>
<component-a></component-a>
<div>random html</div>
<component-b></component-b>
<component-a></component-a>
</my-component>
Then in my-component i can aquire references to all instances of the individual component types:
export class MyContainer implements AfterContentInit {
@ContentChildren(ComponentA) componentAInstances: QueryList<ComponentA>;
@ContentChildren(ComponentB) componentBInstances: QueryList<ComponentB>;
ngAfterContentInit() {
console.log('count of component A: ' + this.componentAInstances.length);
console.log('count of component B: ' + this.componentBInstances.length);
}
}
And the console will show that there are 2 component A's and 1 component B.
I should point out that this is at release candidate 4.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…