4

I am trying to use the component life cycle hooks that are exposed by Angular 4 to process certain information related to the component.
When I tried to search I was able to find the list of life cycle hooks that are exposed by angular 4 (https://angular.io/guide/lifecycle-hooks)
I am more interested in the code snippet that triggers few of these methods. For example in Java following lines of code would trigger the constructor in 'String' class:

String hotelName = new String("Marriott");

What would be the HTML, JavaScript code that would trigger following life cycle methods:

  • constructor
  • ngOnInit
  • ngAfterViewInit
  • ngAfterViewChecked
  • ngOnDestroy

Thanks in advance.

Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
shekharlondhe
  • 79
  • 1
  • 8
  • Lifecycle hooks are bundled to compile output during process of compiling source (components, classes, services ...) Component is angular directive and if they implement lifecycle hooks (interface) produced source output calls implemented interface methods. Only constructor is part of typescript class. Source of compile module is: https://github.com/angular/angular/tree/master/packages/compiler. Only one thing this is not angularjs (old pure js version), it is just angular. – MRsa Nov 19 '17 at 12:25
  • `constructor` is no lifecycle hook. As in Java it is called when an instance is created with `new`. The other lifecycle hooks are invoked by Angular when it creates a component or when it runs change detection. – Günter Zöchbauer Nov 19 '17 at 15:24
  • hey, is there anything unclear about [my answer](https://stackoverflow.com/a/47380362/2545680)? – Max Koretskyi Nov 23 '17 at 08:54
  • @AngularInDepth.com thank you very much for your reply. I was not able to go through all the links shared by you but, I think I have got my answer. – shekharlondhe Nov 24 '17 at 09:22

1 Answers1

4

In Angular all lifecycle hooks are triggered as part of change detection by the framework. To learn more about the process read:

For example in Java following lines of code would trigger the constructor in 'String' class:

Constructor is not a lifecycle hook and is triggered when a component is created. It's similar mechanics to the one you showed which includes calling a component constructor with new.

To learn more read:

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488