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

查看:101
本文介绍了我可以防止/延迟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天全站免登陆