Skip to main content

Lifecycle overview, constructor and the first hooks

Learn Lifecycle overview, constructor and the first hooks (Module 9 · Component Lifecycle, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

The life of a component • Think of a component like an employee : hired (created), onboarded (inputs set), working (views rendered and updated), and finally leaves (destroyed). Angular's component lifecycle lets you run code at each of those moments through lifecycle hooks : methods with fixed names such as ngOnInit . • The constructor is plain TypeScript (like a Java constructor). It runs first, before Angular has set any @Input . Use it only to receive dependencies (or use inject() ) and set simple fields. • ngOnInit() runs once, after the first inputs are set. This is where you load data...

More lessons in Module 9 · Component Lifecycle