我正在使用Angular2。我有一個div:setTimeOut for elementRef
<div class="saved" #saved>
<p>Saved</p>
<i class="fa fa-check" aria-hidden="true"></i>
</div>
我訪問它在TS:
@ViewChild("saved") public saved: ElementRef;
接下來我顯示該塊:
this.saved.nativeElement.style.display = 'block';
然後,我想幾秒鐘後隱藏它,但是這部分代碼不正確:
setTimeout(this.closeSavedWindow, 3000);
其中
closeSavedWindow() {
this.saved.nativeElement.style.display = 'none';
}
的錯誤是
TypeError: this.saved is undefined
這有內部和外部的功能,兩個不同的上下文可以調試此使用的console.log(本) closeSavedWindoe() –