To my understanding ng-show, like other bindings, should stop calling the associated method once the digest stabilizes. With that in mind I would expect to see the following console.log() twice. It is however logging once a second or so.
据我所知,ng-show和其他绑定一样,一旦摘要稳定,就应该停止调用相关的方法。考虑到这一点,我希望看到以下console.log()两次。然而,它每秒记录一次。
Is my understanding flawed, my implementation, or is this functioning as expected and I shouldn't worry about any negative performance impacts of such methods being called continuously?
我的理解是否有缺陷,我的实施,或者这是否按预期运作,我不应该担心这些方法的任何负面性能影响被连续调用?
The method (in CoffeeScript)
方法(在CoffeeScript中)
$scope.showThis = ->
console.log("foobar")
true
The HTML tag with the ng-show
带有ng-show的HTML标记
<div ng-show="showThis()">hey, you can see me!</div>
Thanks for any insights =]
感谢您的任何见解=]
2 个解决方案
#1
7
It is your correct understanding that ng-show would be called once the digest is 'stabilzed'. However, what you maybe fail to understand is that the apply digest cycle might be triggered by many things and so your ng-show would be called for this scope many times. You can debug and check that this scope's apply/digest is being called exactly as many times as your ng-show's method. There are no guarantees it should be called only once, twice or whatever times. As soon as a digest/apply cycle is triggered on your scope, you gonna get your console.log. Simple as that.
正确的理解是,一旦摘要“稳定”,就会调用ng-show。但是,您可能无法理解的是,应用摘要周期可能会被许多事情触发,因此您的ng-show将被多次调用此范围。您可以调试并检查此范围的apply / digest的调用次数与ng-show的方法完全相同。无法保证只应调用一次,两次或任何时间。只要在您的示波器上触发摘要/应用周期,您就会获得console.log。就那么简单。
Of course, the reasons for triggering a digest/apply cycle might be multiple, but in my opinion eventually it should stop if you don't trigger browser events or don't do reloads or don't do some $timeout stuff. If it doesn't stop, then you messed up somewhere.
当然,触发摘要/应用周期的原因可能是多次,但在我看来,如果你不触发浏览器事件或不做重新加载或者不做一些超时的事情,它最终应该停止。如果它没有停止,那么你搞砸了某个地方。
I created a Plunkr for you so you can check that in the normal case, it would be called once or twice and if you don't act on it, nothing happens. If you, however press the button, which updates a totally different scope value, it would trigger a scope digest/apply cycle and you would get an additional console.log:
我为你创建了一个Plunkr,所以你可以在正常的情况下检查它,它会被调用一次或两次,如果你不采取行动,没有任何反应。但是,如果您按下更新完全不同的范围值的按钮,则会触发范围摘要/应用周期,您将获得一个额外的console.log:
#2
2
The way the digest cycle works is that every time a $apply() or $digest()
is called it will go through all the watches that are setup (and expressions in directives are usually implemented by watches). If ANY watch fired, then it will run through ALL the watches again. It will keep doing this again and again until no more watches fire or until it's gone through 10 loops (at which point it throws an exception and fails).
摘要循环的工作方式是每次调用$ apply()或$ digest()时,它将遍历所有设置的监视(指令中的表达式通常由监视器实现)。如果任何手表被解雇,那么它将再次通过所有手表。它将一次又一次地继续这样做,直到不再看到它或直到它经历了10个循环(此时它抛出异常并失败)。
Most any state change in angular will trigger a digest cycle (such as clicking on things, updating input values, or other events). You can expect watches to be called many many times throughout the lifetime of your application, this is why you need to make sure that you don't do any heavy computation in them.
大多数状态的角度变化都会触发摘要周期(例如点击事物,更新输入值或其他事件)。您可以期望在应用程序的整个生命周期中多次调用手表,这就是为什么您需要确保不对其进行任何繁重的计算。
#1
7
It is your correct understanding that ng-show would be called once the digest is 'stabilzed'. However, what you maybe fail to understand is that the apply digest cycle might be triggered by many things and so your ng-show would be called for this scope many times. You can debug and check that this scope's apply/digest is being called exactly as many times as your ng-show's method. There are no guarantees it should be called only once, twice or whatever times. As soon as a digest/apply cycle is triggered on your scope, you gonna get your console.log. Simple as that.
正确的理解是,一旦摘要“稳定”,就会调用ng-show。但是,您可能无法理解的是,应用摘要周期可能会被许多事情触发,因此您的ng-show将被多次调用此范围。您可以调试并检查此范围的apply / digest的调用次数与ng-show的方法完全相同。无法保证只应调用一次,两次或任何时间。只要在您的示波器上触发摘要/应用周期,您就会获得console.log。就那么简单。
Of course, the reasons for triggering a digest/apply cycle might be multiple, but in my opinion eventually it should stop if you don't trigger browser events or don't do reloads or don't do some $timeout stuff. If it doesn't stop, then you messed up somewhere.
当然,触发摘要/应用周期的原因可能是多次,但在我看来,如果你不触发浏览器事件或不做重新加载或者不做一些超时的事情,它最终应该停止。如果它没有停止,那么你搞砸了某个地方。
I created a Plunkr for you so you can check that in the normal case, it would be called once or twice and if you don't act on it, nothing happens. If you, however press the button, which updates a totally different scope value, it would trigger a scope digest/apply cycle and you would get an additional console.log:
我为你创建了一个Plunkr,所以你可以在正常的情况下检查它,它会被调用一次或两次,如果你不采取行动,没有任何反应。但是,如果您按下更新完全不同的范围值的按钮,则会触发范围摘要/应用周期,您将获得一个额外的console.log:
#2
2
The way the digest cycle works is that every time a $apply() or $digest()
is called it will go through all the watches that are setup (and expressions in directives are usually implemented by watches). If ANY watch fired, then it will run through ALL the watches again. It will keep doing this again and again until no more watches fire or until it's gone through 10 loops (at which point it throws an exception and fails).
摘要循环的工作方式是每次调用$ apply()或$ digest()时,它将遍历所有设置的监视(指令中的表达式通常由监视器实现)。如果任何手表被解雇,那么它将再次通过所有手表。它将一次又一次地继续这样做,直到不再看到它或直到它经历了10个循环(此时它抛出异常并失败)。
Most any state change in angular will trigger a digest cycle (such as clicking on things, updating input values, or other events). You can expect watches to be called many many times throughout the lifetime of your application, this is why you need to make sure that you don't do any heavy computation in them.
大多数状态的角度变化都会触发摘要周期(例如点击事物,更新输入值或其他事件)。您可以期望在应用程序的整个生命周期中多次调用手表,这就是为什么您需要确保不对其进行任何繁重的计算。