13

i draw a sketch that represent the flow of the component life-cycle

using sketch.io

after finishing the docs about life-cycle hooks
i came up with this sketch. now is this is the right order of the life-cycle hooks in a component?enter image description here

Raed Khalaf
  • 1,997
  • 2
  • 13
  • 29

3 Answers3

21

Here is what I understand from the Angular.io document. It is may help you. Angular life cycle hooks[3]

mansoor
  • 1,553
  • 1
  • 13
  • 18
8

The sketch provided by you is somewhat correct. For your reference, I tried implementing each of the hooks. Just in case to determine their point of occurrence I logged each of them to the console.

For your reference, below is the screenshot of my console as I allow my application to load and follow each lifecycle one after the other.

When the page loads for the first time.

This is the console when the page loads for the firsts time.

Therefore, the components thus called during this lifecycle have their own functionalities:

1)Constructor: A default method which is called when the class is instantiated.

2)ngOnChanges: Executes when a new component is created, when one of the bound properties with @Input changes, also it is the only hook that takes an argument when it is called which is called as SimpleChanges.

3)ngOnInit: Called once the component is initialized. This doesn't allow the component to be visible over the DOM. This runs just after the constructor.

4)ngDoCheck: Runs when change detection runs. It also runs if there is no change and even if it is just an event that occurred, just in case to make sure if there is something that has changed. (for eg: It will run after a button click event irrespective of that it is making ant change or not) 5)ngAfterContentInit: This is called after content(ng-content) has been projected into the view.

6)ngAfterContentChecked: This is called after every projected content has been checked.

7)ngAfterViewInit: Called after the components view (and child view) has been initialized.

8)ngAfterViewChecked: Called every time the view (and child view) has been checked.

In the image below, look for the console when I click over the 'Recipe Book' navbar heading which is a link at the top left corner:

When clicked over the navbar heading.

We can clearly see 'ngDoCheck' being called followed by 'ngAfterContentChecked' and then 'ngViewChecked' at the last. If you notice 'ngAfterContentInit' and 'ngViewInit' are only called at the starting when the content has been finally projected to the view. They have nothing to do with the corresponding change that occurs.

9)ngOnDestroy: Called when we generally use an if condition and render the component accordingly. This is mainly called right before the object is destroyed by the angular.

Refer to the below image and look for one of the Recipe information cards that vanishes as I click over the 'Destroy first Component' button:

ngDestroy called as I hit the Destroy button.

Here 'ngOnDestroy' is called followed by the 'ngDoCheck' and other following hooks. This will tend the loop to be called once again but then the destroyed content won't get rendered. (FYI, here I have just used a splice method in order to destroy one of the rendered content)

I can also provide the code if needed, hope this might help you as well as others to understand the functioning of the lifecycle hooks in Angular. :)

P.S. Please ignore the rest of the elements over that page screenshots.

Community
  • 1
  • 1
Shivam Shukla
  • 131
  • 1
  • 4
  • 3
    isn't it is from Maximilian Schwarzmüller recipe project :D – Kanishk Tanwar Feb 07 '20 at 06:54
  • Yes exactly! The recipe project. :D – Shivam Shukla Feb 07 '20 at 13:53
  • This example doesn't explain the main point, which is WHY IS IT CALLED, why??? Why is it useful, when exactly to use it?? – ieXcept Apr 04 '20 at 11:03
  • Hello @ieXcept, where do you see Mr. Raed Khalaf questioning about Why and when the life cycle hook needs to be called? His intension was to verify if the sketch provided by him is correct or not. That's what I've stated in my answer with all the understandings and screenshots. Coming to your question that when these should be called totally depends on 'your' use case. I suggest you have a sheer understanding of these hooks. This might help you answer your questions on your own. Thanks! :) – Shivam Shukla Apr 19 '20 at 12:12
3

ngOnChanges() isn't called after the constructor, it's called after change detection is run and change detection updated an @Input().
Also ngDoCheck() is called when change detection is run.
ngOnInit() is called after ngOnChanges() was called the first time.

See also https://angular.io/guide/lifecycle-hooks

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    ngOnChanges() is called whenever the '@Input' changed, after the constructor is executed, Angular sets the '@Input' data, which means trigger the ngOnChanges(). – Raed Khalaf Jun 20 '17 at 08:54
  • I think this is misleading. Angular runs change detection on a component after it created a component. If there are input bindings they will be updated and therefore `ngOnChanges()` will be called. `ngOnChanges()` is unrelated to the constructor. – Günter Zöchbauer Jun 20 '17 at 09:01
  • exactly it's unrelated to the constructor, but the order of execution, will be constructor, onChanges, then OnInit – Raed Khalaf Jun 20 '17 at 09:04
  • If you're only interested in the pure timely order, then yes, but I don't think it's a good idea to memorize it that way. I'm more for understanding than just memorizing. Everone has his own way though, therefore just my opinion. – Günter Zöchbauer Jun 20 '17 at 09:07
  • 3
    If there are no `@Input()`s in a component, then `ngOnChanges()` shouldn't be called (not verified myself yet though). – Günter Zöchbauer Jun 20 '17 at 09:08
  • i'm not "ONLY" interested in the pure timely order, i'm just investigating life-cycle hooks from different perspective, thanks so much for help :) – Raed Khalaf Jun 20 '17 at 09:16
  • i try it, if there is no '@Input()' then ngOnChanges() wont be called – Raed Khalaf Jun 20 '17 at 09:17
  • @Raed Are you 100% sure that `ngOnChanges() ` isn't invoked when no @Input()s? – ieXcept Apr 04 '20 at 10:54