Angular ContentChildren(Component) get any type

Viewed 2996

I know you can get the children by specifying a specific component type @ContentChildren(SpecificComponent) but can I use @ContentChildren(SOMETHING) and get a component regardless of its type, like a generic component?

2 Answers

Not sure why someone downvoted this without saying why but ended up finding the solution:

@ContentChildren('childComponent') childComponents;

I pass a string into the contentchildren instead of a component. The string allows me to use a reference a selector (#childComponent)

@Component({
  template: '<ng-content></ng-content>',
})
export class TestComponent implements AfterContentInit {

    constructor(private readonly elementRef: ElementRef) {
    }

    ngAfterContentInit() {
        const element = this.elementRef.nativeElement as HTMLElement;
        // do wathever you want with element.children
    }
}
Related