>

CODEMASTERYLAB

IDE PLATFORM
CoursesCommunity BlogsKnowledge GraphQuizzesInterview Prep

Sign in to track progress & earn XP

Sign In Create Account
>
Code Mastery Lab

Code Mastery Lab

Sign In

Advanced Components — Host Elements, Content Projection, and Lifecycle Hooks

Question 1 / 1

0:00
Core
Medium

A TabsetComponent uses @ContentChildren(TabComponent) to query projected tabs. In ngOnInit, it tries to activate the first tab. In ngAfterContentInit, the same code works correctly. Why does it fail in ngOnInit?

typescript export class TabsetComponent implements OnInit, AfterContentInit { @ContentChildren(TabComponent) tabs: QueryList<TabComponent>;

ngOnInit(): void { console.log(this.tabs.length); // logs 0 this.tabs.first?.isActive = true; // does nothing }

ngAfterContentInit(): void { console.log(this.tabs.length); // logs 3 this.tabs.first.isActive = true; // works correctly } }