我有一個使用Material Design Lite的angular2組件,但複選框(也可能是其他人)在第一次加載組件時沒有正確呈現,而如果我與複選框交互,mdl樣式已正確應用。在我的組件我有在生產代碼中使用setTimeout是否安全?
ngAfterViewInit() {
componentHandler.upgradeDom();
}
,但不能解決我的問題,所以我用的setTimeout要執行的服務已經恢復了一些數據之後。這似乎工作,但它是否建議這樣做?
this.service.getDetails(this.id)
.finally(() => {
setTimeout(() => {
componentHandler.upgradeDom();
}, 10);
})
.subscribe((details) => {
this.details = details;
});
它在那裏,所以它可以使用。它就在你想要使用的地方以及如何! – Smit
也許你可以試試'changeDetection:ChangeDetectionStrategy.OnPush'.你可以在[Change Detection Explained]中找到一些有用的信息(https://blog.thoughtram.io/angular/2016/02/22/angular-2-change - 檢測 - explained.html) –