Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 -- ngAfterViewChecked is called many times..How to call a method just once after DOM is completely loaded?

I need to apply jquery plugin to radio buttons in Angular2 using Typescript.

If I assign in ngAfterViewChecked, it is called many times and the control is refreshed multiple times.

What is the alternate solution for calling the javascript method after DOM is ready?

like image 302
Kcs Avatar asked Dec 30 '16 06:12

Kcs


People also ask

Why is ngAfterViewChecked called multiple times?

ngAfterViewChecked() would be invoked once the DOM tree get any change. So if the DOM tree got change for many times, the ngAfterViewChecked() method would be invoked many times.

How many times ngAfterViewChecked is called?

ngAfterViewChecked() is called after ngAfterContentInit. ngAfterViewChecked() is called after every subsequent ngAfterContentChecked. Triggering the clickMe() function will trigger ngAfterViewChecked().

Can ngAfterViewInit be called multiple times?

ngAfterViewInit()link A callback method that is invoked immediately after Angular has completed initialization of a component's view. It is invoked only once when the view is instantiated.

What is the use of ngAfterViewChecked?

ngAfterViewChecked()linkA callback method that is invoked immediately after the default change detector has completed one change-check cycle for a component's view.


2 Answers

Try ngAfterViewInit and have a look here.

like image 76
SeleM Avatar answered Oct 20 '22 06:10

SeleM


ngAfterViewChecked() would be invoked once the DOM tree get any change.

So if the DOM tree got change for many times, the ngAfterViewChecked() method would be invoked many times.

Suggest not to put business logic in this method. But only the screen refresh related logic instead, like to scroll the window to bottom if new message is coming in.

like image 34
Richard Avatar answered Oct 20 '22 06:10

Richard