Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142129

Need a callback when it is OK to access the DOM

$
0
0

I have an Angular component that fetches markdown, converts it to HTML, then inserts it into the DOM. After insertion it does some more DOM manipulation. So the template is this:

<div id="fixroot" [innerHTML]="content"></div>

and the component is like this:

    content: string;

    ...

    async getPage(url: string) {
       try { 
           this.content = this.mdToHtml(await this.http.get(url, { responseType: 'text'}).toPromise();
           setTimeout(() => this.fixupDOM(), 400);

    ....

    fixupDom() {
       const el = document.getElementById('fixroot');
       // do stuff to the DOM element that are children of el


The use of setTimeout here is really awful, but without it the fixupDom method doesn't work because the DOM is not ready when the Promise concludes. But it has to go.

What I need is a callback (probably from the HTMLElement el) that calls my routine as soon as the DOM structure is ready. I haven't been able to make anything work. Anyone know the right function?


Viewing all articles
Browse latest Browse all 142129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>