Developer Strange
Developer Strange

Reputation: 2198

What is the difference between ngAfterViewInit() and ngAfterViewChecked()?

I am learning Angular 2. In the angular 2 lifecycle hooks

ngAfterContentInit -- Component content has been initialized
ngAfterContentChecked -- Component content has been Checked
ngAfterViewInit -- Component views are initialized
ngAfterViewChecked -- Component views have been checked

I can't understand the difference between the ngAfterContentInit Vs ngAfterContentChecked, ngAfterViewInit Vs ngAfterViewChecked.

They mentioned Component content has been Checked and Component views have been checked. I can't understand what that word "Checked" mentioned?

Can any one explain.

Upvotes: 45

Views: 70717

Answers (3)

Max Koretskyi
Max Koretskyi

Reputation: 105517

The best article out there that explains lifecycle hooks in details is Everything you need to know about change detection in Angular.

ngAfterViewInit Vs ngAfterVIewChecked

As explained in the article the ngAfterVIewChecked is called every time Angular has finished running change detection on a component and it's children. ngAfterViewInit is called only during first change detection cycle. You can use it if you need to know when the first change detection cycle runs. For example, you need to setup listeners for some jQuery elements and you need to wait until they are initialized:

ngAfterViewInit() {
  this.widget = $(this.location.nativeElement).slider({value: this.value});

  this.widget.on('slidestop', (event, ui) => {
    this.onChange(ui.value);
  });
}

The same holds for ngAfterContentInit with the difference that Angular runs change detection for projected content (through ng-content) instead of the children specified in the components template.

I can't understand what that word "Checked" mentioned?

Checking means running change detection and performing change detection related operations like DOM update, querylist update and child component bindings update.

Upvotes: 43

Pranjali Rastogi
Pranjali Rastogi

Reputation: 81

ngDoCheck: This is fired each time anything that can trigger change detection has fired (e.g. click handlers, HTTP requests, route changes, etc…). This lifecycle hook is mostly used for debug purposes;

ngAfterContentInit: called after external content has been projected into the component. Remember content queries like @ContentChildren and @ContentChild are set before the hook callback is called;

ngAfterContentChecked: called after every check of component content;

ngAfterViewInit: called after component’s view(s) are initialized. Perfect for initializing event third party library, that needs the view be composed before taking any action (note that a view differs from content because a components view is the non-projected template);

ngAfterViewChecked: called after every check of a component’s view(s);

Upvotes: 5

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

You can refer to the docs which clearly states of these:

ngAfterContentInit()

Respond after Angular projects external content into the component's view. Called once after the first ngDoCheck().

ngAfterContentChecked()

Respond after Angular checks the content projected into the component. Called after the ngAfterContentInit() and every subsequent ngDoCheck().

ngAfterViewInit()

Respond after Angular initializes the component's views and child views. Called once after the first ngAfterContentChecked().

ngAfterViewChecked()

Respond after Angular checks the component's views and child views. Called after the ngAfterViewInit and every subsequent ngAfterContentChecked().

That means Checked states to say it runs after Init. Initialization means it runs at first and Checking for the changes runs many times after initialization.

Upvotes: 21

Related Questions