What are the hooks in the life cycle of angular2up applications

Time:2019-10-19

The component / instruction above angular 2 has lifecycle events and is managed by @ angular / core. Instances of instructions and components have a lifecycle: triggered when angular creates, updates, and destroys them. By implementing the life cycle hook interface defined in one or more angular core libraries, developers can intervene in these critical moments in the life cycle.

Each interface has a unique hooks method, whose name is composed of interface name plus ng prefix. For example, the OnInit method of the ngonint interface, which must be implemented in a component.

Some events apply to components / instructions, while a few apply only to components.

Ngonchanges: responds when angular sets its data binding properties to receive the current and previous object values.
Ngoninit: initializes the component / instruction after the first ngonchange trigger. This is the most common method for retrieving data from the template from the back-end service.
Ngdocheck: detects and executes when the angular context changes. Every time you change the detection run, it is called.
Ngondestroy: clear before an angular destroy instruction / component. Unsubscribe observable objects and detach from event handlers to avoid memory leaks.

Component specific hooks:

Ngaftercontentinit: component content initialized
Ngaftercontentchecked: after angular checks the external content of the binding projected into its view.
Ngafterviewinit: after an angular creates a view of a component.
Ngafterview checked: after angular checks the binding of component view.