DOM完成渲染后执行功能 [英] Execute function after DOM has finished rendering
问题描述
我记得从博客中阅读了以下摘录.
I recall reading the excerpt below from a blog.
$ timeout将新事件添加到浏览器事件队列中(呈现引擎已在此队列中),因此它将在新的超时事件之前完成执行.
$timeout adds a new event to the browser event queue (the rendering engine is already in this queue) so it will complete the execution before the new timeout event.
我想知道在angular/javascript中是否有比使用
I'm wondering if there is a better way in angular/ javascript than using
setTimeout(() => {
// do something after dom finishes rendering
}, 0);
在DOM完全完成诸如更新* ngFor并将结果呈现在页面上之类的任务后执行代码.
to execute code when the DOM has completely finished a task such as updating an *ngFor and rendering the results on the page.
推荐答案
您可以尝试使用ngAfterViewInit
生命周期挂钩,这是按时间顺序排列的最后一次单发生命周期挂钩.
You might try the ngAfterViewInit
life-cycle hook, which is the chronologically last single-fire life-cycle hook.
https://angular.io/guide/lifecycle-hooks
它的工作原理与ngInit
相似,但是在视图和子视图初始化之后会触发.
It works much like ngInit
but it fires after the view and child views have initialized.
如果您需要每次DOM完成时都会触发的内容,则可以尝试ngAfterViewChecked
或ngAfterContentChecked
.
If you need something that fires every time the DOM finishes you can try ngAfterViewChecked
or ngAfterContentChecked
.
这篇关于DOM完成渲染后执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!