tezvyn:

What is ngOnInit's purpose and why prefer it over the constructor?

Source: angular.devbeginner

Tests Angular lifecycle timing and input binding. Strong answer: ngOnInit runs once after inputs are initialized, but the constructor instantiates the class before bindings exist.

Tests deep understanding of Angular's component lifecycle and the boundary between class instantiation and framework initialization. A strong answer explains that the constructor runs when JavaScript instantiates the class before Angular binds inputs, while ngOnInit runs exactly once after all inputs receive initial values, making it safe for input-dependent setup. It notes that the first ngOnChanges precedes ngOnInit.

Read the original → angular.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

What is ngOnInit's purpose and why prefer it over the constructor? · Tezvyn