我可以在模型更新时阻止/延迟 AngularJS $digest 的发生吗 [英] Can I prevent / delay the AngularJS $digest from happening when model is updated

查看:16
本文介绍了我可以在模型更新时阻止/延迟 AngularJS $digest 的发生吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法推迟或延迟摘要的发生?

Is there a way to postpone or delay a digest from happening?

我想对模型进行大量更改,但我不希望在对模型进行所有更改之前触发摘要.模型上的某些对象具有观察者,可以更新模型上的其他对象以进行更改.

I have a bunch of changes that I want to make to a model but I don't want the digest to fire until all changes to the model were made. Some of the objects on the model have watchers that update other objects on the model to change.

理想情况下我想

  • 停止 $digest
  • 对模型进行所有更改
  • 开始 $digest

$digest 将查找所有脏对象并触发观察者.

The $digest will find all dirty objects and fire the watchers.

另一个解决方案是,我可以不停止 $digest

Another solution to this is to, instead of stopping $digest I could

  • 移除观察者
  • 对模型进行所有更改(摘要仍在运行)
  • 添加被移除的观察者

添加观察者后,我需要运行观察者方法以确保模型处于正确状态.

After the watchers are added I'd need to run the watcher methods to ensure that the model is in the correct state.

我只是觉得第二个选项看起来像是一个黑客.

I just feel the 2nd option seems like its a hack.

想法??

推荐答案

这根本不是黑客.这是一个很好的问题,因为当用户快速输入文本或按住退格键时,大型数据集会导致 $digest 循环运行非常缓慢.你绝对可以像被小心你的 $watch$filter 函数,但有时使用 去抖动功能.

This is not a hack at all. Its a good question because large data sets can cause the $digest cycle to run very slowly when a user inputs text rapidly or holds down backspace. You can definitely do performance tweaks like being careful with your $watch and $filter functions, but sometimes its a better idea to delay the $digest cycle using a debounce function.

这篇关于我可以在模型更新时阻止/延迟 AngularJS $digest 的发生吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆