I'm populating an ng-content with some async values and I can see my content printed in the HTML but can't access to the ContentChildren from the component code.
// form.component.ts
@Component({
template: `<dropdown>
<option class="p-2" value="">Choose your Business</option>
<option
class="p-2"
*ngFor="let business of businesses$ | async"
[attr.value]="business.id">{{ business.name }}</option>
</dropdown>`
...
})
class FormComponent {
businesses$ = ...
}
// dropdown.component.ts
@Component({
selector: 'dropdown',
template: '<select><ng-content selector="option"></ng-content></select>',
...
})
class DropdownComponent implements AfterContentInit {
@ContentChildren(HTMLOptionElement) options: QueryList<HTMLOptionElement>;
ngAfterContentInit() {
console.log(this.options.length); // it always returns 0
this.options.changes.subscribe((changes) => {
console.log('After Changes: ', changes.length); // this part is never executed
});
}
}
The select is printed with the options passed but the code never captures the options in the ContentChildren.