AngularJS: What is Angular Lifecycle and Lifecycle Hook methods?

 Component Lifecycle

The three phases are:  creates, updates, and destroys.

A component instance has a lifecycle that starts when Angular instantiates the component class and renders the component view along with its child views. The lifecycle continues with change detection, as Angular checks to see when data-bound properties change, and updates both the view and the component instance as needed. The lifecycle ends when Angular destroys the component instance and removes its rendered template from the DOM. Directives have a similar lifecycle, as Angular:  creates, updates, and destroys instances in the course of execution.

 Component Lifecycle Hook

Respond to events in the lifecycle of a component or directive by implementing one or more of the lifecycle hook interfaces in the Angular core library. The hooks give you the opportunity to act on a component or directive instance at the appropriate moment, as Angular creates, updates, or destroys that instance.

After your application instantiates a component or directive by calling its constructor, Angular calls the hook methods you have implemented at the appropriate point in the lifecycle of that instance.

Angular executes hook methods in the following sequence. Use them to perform the following kinds of operations.

STT hook method Details
1 ngOnChanges

When an input or output binding value changes.

Respond when Angular sets or resets data-bound input properties. This happens frequently, so any operation you perform here impacts performance significantly.

Called before ngOnInit() (if the component has bound inputs) and whenever one or more data-bound input properties change.

2 ngOnInit

After the first ngOnChanges.

Initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component's input properties.

Called once, after the first ngOnChanges(). ngOnInit() is still called even when ngOnChanges() is not (which is the case when there are no template-bound inputs).

3 ngDoCheck

Developer's custom change detection.

Detect and act upon changes that Angular can't or won't detect on its own.

Called immediately after ngOnChanges() on every change detection run, and immediately after ngOnInit() on the first run.

4 ngAfterContentInit

After component content initialized.

Respond after Angular projects external content into the component's view, or into the view that a directive is in.

Called once after the first ngDoCheck().

5 ngAfterContentChecked

After every check of component content.

Respond after Angular checks the content projected into the directive or component.

Called after ngAfterContentInit() and every subsequent ngDoCheck().

6 ngAfterViewInit

After the views of a component are initialized.

Respond after Angular initializes the component's views and child views, or the view that contains the directive.

Called once after the first ngAfterContentChecked().

7 ngAfterViewChecked

After every check of the views of a component.

Respond after Angular checks the component's views and child views, or the view that contains the directive.

Called after the ngAfterViewInit() and every subsequent ngAfterContentChecked().

8 ngOnDestroy

Just before the directive is destroyed.

Cleanup just before Angular destroys the directive or component. Unsubscribe Observables and detach event handlers to avoid memory leaks. See details in Cleaning up on instance destruction in this document.

Called immediately before Angular destroys the directive or component.

 

creates

@create /kri:'eit/
* ngoại động từ
- tạo, tạo nên, tạo ra, tạo thành, sáng tạo
- gây ra, làm
- phong tước
=to create a baron+ phong nam tước
- (sân khấu) đóng lần đầu tiên
=to create a part+ đóng vai (kịch tuồng) lần đầu tiên
* nội động từ
- (từ lóng) làm rối lên, làn nhắng lên; hối hả chạy ngược chạy xuôi
=to be always creating about nothing+ lúc nào cũng rối lên vì những chuyện không đâu vào đâu

@create
- tạo ra, tạo thành, chế thành

updates

@update
* ngoại động từ
- làm cho cập nhật, hiện đại hoá
- cho ai thông tin mới nhất (về cái gì)
* danh từ
- sự cập nhật hoá (thông tin mới nhất)