Angular Component Lifecycle Hooks

Angular Component Lifecycle Hooks

Angular lifecycle hooks allows you to implement critical logic at the right time of a component's lifecycle. During the process of component creation, besides the constructor which is mainly responsible for dependency injection and property initialization, there are lots of lifecycle hoods you can add logic to by implementing corresponding lifecycle interfaces.

All possible lifecycle hooks are listed below that may be called in the process of component creation. Note those functions are called in their listed order. Since ngOnChanges is called after the Constructor, the component input properties are not available in the Constructor.

No alt text provided for this image

After a component is created, it enters into a stage of change detection cycle which update itself whenever changes are detected. The following are four lifecycle hooks that runs in the order of being listed.

No alt text provided for this image

When it's time for Angular to destroy a component, a last but very important (you almost always want to implement this.) lifecycle hook will be called. This is ngOnDestroy which you can implement resource clean up logic in it, like unsubscribe Observables and detach event handlers to avoid memory leaks.

To view or add a comment, sign in

More articles by Lixin C.

  • Solution Architect - part 1

    Solution Architects (SA) are responsible for providing solutions to fulfill business requirements by working with…

  • Retrieve Data From Multiple Microservices

    In microservice pattern, microservices are suppose to own their own database to achieve best independent scalability…

  • Chaining HTTP Calls Using Observables With RxJS

    Sometimes we need to make multiple HTTP calls in a specific order to get data. The data from previous call are either…

  • RxJS Concise Demo - Creation Operators

    RxJS operators are functions that can create or process Observables. Creation operators are used to create new…

  • Async Controller Methods For Scalability

    In API action methods, it is very common to call a database or another web resource to get requested data. Getting data…

  • HTTP Status Code Short listed

    Commonly Used HTTP Status Codes for Web Services. 200 => OK, general indication of request success.

  • Network Layers for Software Engineers

    Sending messages to another person using web browser or mobile app is not surprisingly easy nowadays and most people…

  • Implementing a Fixed-Window Median Data Stream With Heaps

    A data stream is like a pipe with moving data inside. Here I only consider integer numbers as data and try to find the…

  • Test Your Testing Strategy with A Dice Game

    1. Project background Sometime in last year I got interested in how does the 3D web rendering engines work.

Explore content categories