I would like to access the contents of ng-content, to start with just to get a count of content elements, but with more awesomeness later.
For example:
@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 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 AfterContentInit {
@ContentChildren(???) containerItems : QueryList<???>;
ngAfterContentInit() {
console.log('after content init: ' + this.containerItems.length);
}
}
What do I put as the type for the content children?
They could be anything. I tried ElementRef, but that didn't work and always gave a count of zero.
Example of use:
<my-container>
<div>some content here</div>
<another-component [title]="'another component there'"></another-component>
<div>there's lots of content everywhere</div>
<span>no assumptions would be fair</span>
</my-container>
I would expect to see 'after content init: 4'.
See Question&Answers more detail:os