我有一个使用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) –