角范围函数执行两次当它应该只运行一次? [英] Angular scope function executed twice when it should only run once?

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

问题描述

演示这里

快速的问题:?在下面的code,我只调用该函数 isSpecificPage()一次,为什么两次CONSOLE.LOG

 < D​​IV NG隐藏=isSpecificPage()>
  < P>您好!< / P>
< / DIV>


解决方案

角把手表上的 NG-隐藏功能,使每一个消化周期可以看到,如果结果改变了(因此,如果它需要从隐藏到显示的元素或反之亦然更改)。

在观看功能评估(在 $消化),如果其中任何已从previous改变 $消化然后角知道变化会波及通过其他观看功能(也许改变的变量在另一个观看功能使用)。所以每腕表重新评估(也叫脏处理),直到没有任何的改变手表的结果。因此,通常你会看到两个通话每消化功能看,有时多个(最多10圈10-通过它放弃并报告错误说,它不能稳定)。

下面是更多关于消化


  

http://docs.angularjs.org/api/ng 。$ rootScope.Scope


  
  

HTTP:// WWW。 benlesh.com/2013/08/angularjs-watch-digest-and-apply-oh-my.html


Demo here

Quick question: in the following code I only call the function isSpecificPage() once, why it console.log twice?

<div ng-hide="isSpecificPage()">
  <p>Hello!</p>
</div>

解决方案

Angular puts a watch on your ng-hide function so that every digest cycle it can see if the results changed (and thus if it needs to change from hiding to showing your element or vice-versa).

When watched functions are evaluated (during $digest) if any of them have changed from the previous $digest then Angular knows that change might ripple through to other watched functions (perhaps the changed variable is used in another watched function). So every watch is re-evaluated (also called dirty processing) until none of the watches results in a change. Thus typically you'll see 2 calls to watched functions per digest and sometimes more (up to 10- at 10 loops through it gives up and reports an error saying it can't stabilize).

Here's more on watch and digest:

http://docs.angularjs.org/api/ng.$rootScope.Scope

http://www.benlesh.com/2013/08/angularjs-watch-digest-and-apply-oh-my.html

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

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