gpt4 book ai didi

javascript - Angular ng-repeat 缓存(避免在状态更改时重新渲染)

转载 作者:行者123 更新时间:2023-11-30 16:47:43 26 4
gpt4 key购买 nike

我们在 Angular 应用程序中使用 ng-repeat 时出现了巨大的渲染峰值。主页显示大量封面图片(“::”和“track by”就位)。在第一次加载时,它可以正常工作。

但是,如果用户更改状态(我们使用 UI-Router)然后返回主页,那么在桌面上大约有 2 秒的延迟,在移动设备上最多有 10 秒的延迟。

它应该是即时的:所有 json 查询都被缓存。并且 ng-repeat 已经渲染了该内容一次。

作为临时解决方案,我们使用 Angular ux 数据网格 (https://github.com/obogo/ux-angularjs-datagrid)它使返回首页即时,但它不应该在水平模式下工作。使用专用网格来缓存 ng-repeat(或它在幕后所做的任何事情)似乎有点过分了。

所以问题如下:如何避免 ng-repeat 在状态改变时重新渲染内容?

最佳答案

好吧,如果您禁用 ng-repeat 所在的范围。然后它将不再渲染。它本质上变成了静态内容。这使您可以实际控制渲染时间。

ux-datagrid 实际上使用这个概念来关闭 View 之外的 dom,因此 angular 不知道它并且无法渲染它。然后当它在 View 中时将其挂起。

每个作用域都在一个摘要循环中工作。在摘要循环中,它处理范围内的 $watchers。

如果你删除那些观察者,它不会消化它或它的 child 。

这是 ux-datagrid 在其代码中用于激活和停用范围的 2 种方法。您可以将它们复制到另一个对象并将它们用于同一件事。

/**
* ###<a name="deactivateScope">deactivateScope</a>###
* One of the core features to the datagrid's performance is the ability to make only the scopes
* that are in view to render. This deactivates a scope by removing its $$watchers that angular
* uses to know that it needs to digest. Thus inactivating the row. We also remove all watchers from
* child scopes recursively storing them on each child in a separate variable to activate later.
* They need to be reactivated before being destroyed for proper cleanup.
* $$childHead and $$nextSibling variables are also updated for angular so that it will not even iterate
* over a scope that is deactivated. It becomes completely hidden from the digest.
* @param {Scope} s
* @param {number} index
* @returns {boolean}
*/
function deactivateScope(s, index) {
// if the scope is not created yet. just skip.
if (s && !isActive(s)) { // do not deactivate one that is already deactivated.
s.$emit(exports.datagrid.events.ON_BEFORE_ROW_DEACTIVATE);
s.$$$watchers = s.$$watchers;
s.$$watchers = [];
s.$$$listenerCount = s.$$listenerCount;
s.$$listenerCount = angular.copy(s.$$$listenerCount);
subtractEvents(s, s.$$$listenerCount);
if (index >= 0) {
s.$$nextSibling = null;
s.$$prevSibling = null;
}
return true;
}
return false;
}

/**
* ###<a name="activateScope">activateScope</a>###
* Taking a scope that is deactivated the watchers that it did have are now stored on $$$watchers and
* can be put back to $$watchers so angular will pick up this scope on a digest. This is done recursively
* though child scopes as well to activate them. It also updates the linking $$childHead and $$nextSiblings
* to fully make sure the scope is as if it was before it was deactivated.
* @param {Scope} s
* @param {number} index
* @returns {boolean}
*/
function activateScope(s, index) {
if (s && s.$$$watchers) { // do not activate one that is already active.
s.$$watchers = s.$$$watchers;
delete s.$$$watchers;
addEvents(s, s.$$$listenerCount);
delete s.$$$listenerCount;
if (index >= 0) {
s.$$nextSibling = scopes[index + 1];
s.$$prevSibling = scopes[index - 1];
s.$parent = scope;
}
s.$emit(exports.datagrid.events.ON_AFTER_ROW_ACTIVATE);
return true;
}
return !!(s && !s.$$$watchers); // if it is active or not.
}

我不确定这是否会完全回答您的问题,因为您使用的是 UI-Router。如果 View 被重新创建并且没有被缓存,那么它仍然会在编译中重新渲染它。但是,如果不是这样,这不仅会监视一次,当您禁用此范围时,它也会禁用该范围的所有子级。本质上是将它从摘要和所有子节点中分离出来。

重新启用它会将它们全部添加回来。所以你真的关闭了 ng-repeat 和其中的所有内容,只需调用一次停用。它会一直保持静态,直到您重新启用它。

关于javascript - Angular ng-repeat 缓存(避免在状态更改时重新渲染),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com