I need to manipulate a DOM element that is hidden by a *ngIf
directive:
<div *ngIf="myBoolean">
<canvas id="myElement"></canvas>
</div>
After a button click, I run the following method:
showMyElement(){
this.myBoolean = true; //This shows the div containing myElement
console.log(document.getElementById('myElement')); //This gives me null
}
But if I get myElement
by clicking on another button after it is visible, I don't get null
. I guess that when I get it the first time, it hasn't been added to the DOM yet. How can I know when myElement
has been added to the DOM? Is there something like (ngModelChange)
but for when added to the DOM? Thanks!