How to know when DOM is loaded?

17 views
Skip to first unread message

Frank Mehlhop

unread,
Mar 19, 2018, 8:16:34 AM3/19/18
to Angular and AngularJS discussion
Hi,

at my angular app inside a compenent.ts I want to know when the DOM is ready loaded.
So I put inside the constructer following code:
window.addEventListener('load', function () {
console.log('DOM fully loaded and parsed !!!!!!!!!!!!!!!!!!!!!!!!!!!!');
}, false);

The log ""DOM fully loaded and parsed !!!!!!" 
is never called at the browser console.

Why it is not working?
How it should look like to get it work?

Frank

Sander Elias

unread,
Mar 20, 2018, 12:32:41 AM3/20/18
to Angular and AngularJS discussion
Hi Frank,

Doing DOM manipulation with native methods like this is kind of an antipattern in Angular.
Aside from that, if you need to run some code after the template is parsed and read, you should use the ngAfterViewInit life-cycle hook.
Take into account that that is after the change-detection is done, and you can not manipulate data in there.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages