且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角度js'?'在 ng-src 中导致无限循环

更新时间:2023-11-14 22:16:04

好吧,我认为问题在于 getRandom 函数每次被调用时都会返回不同的值.这是发生的事情:

Well I think the problem is the getRandom function returning a different value every time it's called. Here's what happens:

  1. Angular 调用你的函数
  2. 获取值
  3. 看看它与之前的值不同
  4. 将循环标记为脏
  5. 循环结束后,它重新运行循环,从而获得一个新值......

这个想法是让 getRandom 在一段时间内给出谨慎的值.例如,仅在您认为合适的情况下每 1、10、30 秒给出一个新值.

The idea would be to have getRandom give discreet values over a period of time. For example only give a new value every 1, 10, 30 sec whatever you see fit.

这个建议实际上适用于很多事情.Angular 不保证调用绑定中的函数的次数.因此,您需要使它们快速运行,并且您还需要确保对于相同的输入,它们返回相同的输出(大多数情况下,尽管在这种情况下可能是合理的).

This advice applies to many things, actually. Angular provides no guarantees about how many times it will call your functions that are found in bindings. So you need to make them fast and also you need to make sure that for the same input they return the same output(most times, though in this case it might be justified).

还要准确考虑图像何时/如何更改,以及是否可以通过其他方式触发实际更改,请考虑仅在触发实际更改时才为 getRandom 创建新的结果值(例如:用户上传新的个人资料图片,计时器执行等)

Also consider exactly when/how the image might change and if the actual changes can be triggered by something else consider only creating a new result value for getRandom only when the actual changes are triggered(ex: user uploads a new profile image, a timer executes, etc)

更新:使用 Chrome 无法在 plunker 中复制它

UPDATE: Can't replicate it in plunker using Chrome