Monday, 15 January 2024

Angular component life cycles.

In Angular, components have a lifecycle that consists of various stages from the creation of the component to its destruction. These lifecycle stages allow developers to hook into different points of a component's existence and execute code accordingly. Here are the main lifecycle hooks in Angular:

  • ngOnChanges:
    • This hook is called when the component receives input properties. It is invoked whenever a bound input property changes.
  • ngOnInit:
    • This hook is called after the component has been initialized. It is a good place to perform component initialization logic, such as retrieving data from a service.
  • ngDoCheck:
    • This hook is called during every change detection run. It allows you to implement custom change detection logic.
  • ngAfterContentInit:
    • This hook is called after Angular has projected external content into the component's view. It is a good place to perform initialization that depends on content projection.
  • ngAfterContentChecked:
    • This hook is called after Angular checks the content projected into the component.
  • ngAfterViewInit:
    • This hook is called after the component's view has been initialized. It is a good place to perform initialization that depends on the view.
  • ngAfterViewChecked:
    • This hook is called after Angular checks the component's view.
  • ngOnDestroy:
    • This hook is called just before the component is destroyed. It is a good place to clean up resources, such as unsubscribing from observables, to prevent memory leaks.

  Here's an example of how these lifecycle hooks are typically implemented in an Angular component:




 

No comments:

Post a Comment