1
我需要操作由一個*ngIf
指令隱藏DOM元素:如何知道在Angular 2中何時將元素添加到DOM?
<div *ngIf="myBoolean">
<canvas id="myElement"></canvas>
</div>
點擊一個按鈕後,我運行下面的方法:
showMyElement(){
this.myBoolean = true; //This shows the div containing myElement
console.log(document.getElementById('myElement')); //This gives me null
}
但是,如果我通過點擊獲得myElement
可見之後的另一個按鈕,我沒有得到null
。我想,當我第一次得到它時,它還沒有被添加到DOM中。如何知道myElement
何時添加到DOM?有沒有像(ngModelChange)
但添加到DOM時?謝謝!
工作就像一個魅力見。非常感謝! –