我有一个使用 Material Design Lite 的 angular2 组件,但复选框(可能还有其他)元素在组件的第一次加载时未正确渲染,而如果我与复选框交互,则正确应用 mdl 样式。在我的组件中我有
ngAfterViewInit() {
componentHandler.upgradeDom();
}
但这并不能解决我的问题,因此我使用了 setTimeout 在服务返回一些数据后立即执行。这似乎可行,但建议这样做吗?
this.service.getDetails(this.id)
.finally(() => {
setTimeout(() => {
componentHandler.upgradeDom();
}, 10);
})
.subscribe((details) => {
this.details = details;
});
最佳答案
如果您也将组件设置添加到问题中,将会有所帮助。查看 angular.io 文档的组件样式部分。我想您所需要的只是将选择器和样式添加到组件的注释中。来自文档:
@Component({
selector: 'hero-app',
template: `
<h1>Tour of Heroes</h1>
<hero-app-main [hero]=hero></hero-app-main>`,
styles: ['h1 { font-weight: normal; }']
})
export class HeroAppComponent {
/* . . . */
}
关于javascript - 在生产代码中使用 setTimeout 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553706/