执行角范围函数多次 [英] Angular scope function executed multiple times

查看:86
本文介绍了执行角范围函数多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义的范围功能。当我把它从内视图{{}}它执行的x次。

I have defined a function on scope. And when I call it from the view inside {{}} it executes X number of times.

控制器

function testCtrl($scope) {
   $scope.myFunc = function(name) {
       return "Hello " + name;
   }
}

HTML

<div>{{myFunc('Joe')}}</div>

您可以看到它在这个例子: http://jsfiddle.net/rbRvD/2/

You can see it in this example: http://jsfiddle.net/rbRvD/2/

或用Plunker: http://plnkr.co/edit/LLQ7cKs2fEoBwv0C5XPE

Or with Plunker: http://plnkr.co/edit/LLQ7cKs2fEoBwv0C5XPE

我想这样做是错误的方式,但为什么它执行这么多次?

I guess this is done the wrong way, but why is it executed so many times?

推荐答案

您函数运行10次。为什么10?为什么不100?

Your function is run 10 times. Why 10? Why not 100?

答案是在文档

手表聆听者可以改变模型,这可能会引发其他
  听众开火。这是通过重新运行观察家,直到取得了不
  检测到的变化。在重新运行迭代限制为10 以prevent的
  无限循环的僵局。

The watch listener may change the model, which may trigger other listeners to fire. This is achieved by rerunning the watchers until no changes are detected. The rerun iteration limit is 10 to prevent an infinite loop deadlock.

当你看到这种情况出现,就意味着你正在改变模型,这样一种方式,角度有重新运行摘要,再次触发的手表。在您的特定情况下,你拨打的更新一个计数器,它显示在页面上的功能。当计数器的值更改它再次运行摘要,它调用更新该计数器功能,等等等等。

When you see this happening, it means you are changing the model in such a way that Angular has to rerun the digest and fire the watches again. In your particular case you are calling a function that updates a counter, which is displayed on the page. When the counter value changes it runs the digest again, which calls the function that updates the counter, etc, etc.

角期望你(实际上是鼓励你)改变模型,让观应对这些变化,而不是周围的其他方法。

Angular expects you (and indeed encourages you) to change the model and let the view respond to those changes, rather than the other way around.

这篇关于执行角范围函数多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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