011. The Sequence of Events
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
A component's life starts with the constructor, followed immediately by ngOnChanges. Once inputs are settled, ngOnInit runs. After the view is composed, ngAfterViewInit fires. This sequence is deterministic. If you try to access a @ViewChild in ngOnInit, it will likely be undefined because the view hasn't been initialized yet. Mastering this order prevents the 'ExpressionChangedAfterItHasBeenCheckedError' and other common pitfalls.
022. Strategic Cleanup
The ngOnDestroy hook is the most important for application health. In a single-page application, components are frequently created and destroyed as the user navigates. If you subscribe to a global stream or set a setInterval in a component, that work continues even after the component is gone unless you manually stop it in ngOnDestroy. This is the single biggest cause of memory leaks and performance degradation in large-scale Angular apps.
?Frequently Asked Questions
What is Angular?
Angular is a platform and framework built by Google for building single-page client applications using HTML and TypeScript.
What is a Component in Angular?
In Angular, a Component is the basic building block of the UI. Each component consists of an HTML template, a TypeScript class for logic, and a CSS styles file.
What is dependency injection in Angular?
Dependency Injection (DI) is a core design pattern in Angular where classes request dependencies (like data services) from external sources rather than creating them directly.
