AngularJS - 使用方法纳克出现连续调用 [英] AngularJS - ng-show using method continuously called

查看:84
本文介绍了AngularJS - 使用方法纳克出现连续调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解NG-秀,像其他绑定,应停止调用相关的方法,一旦消化稳定。考虑到这一点我希望看到下面的console.log()的两倍。然而,可以记录一次一秒钟左右。

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.

我的理解是有缺陷的,我的实现,或者是这个功能如预期,我不应该担心这些方法的任何负面性能影响正在不断叫什么名字?

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?

的方法(在CoffeeScript中)

$scope.showThis = ->
    console.log("foobar")
    true

HTML标记与NG-表演

<div ng-show="showThis()">hey, you can see me!</div>

感谢您的任何见解=]

Thanks for any insights =]

推荐答案

这是你的正确的认识,一旦摘要是'stabilzed'NG-节目将被调用。不过,你也许不明白的是,申请消化周期可能会被很多事情被触发,所以你的NG-节目将被称为这个范围很多次。您可以调试,并检查此范围内的应用/摘要是被称为完全一样多次你NG-节目的方法。有没有保证,应该只调用一次,两次或任何时候。只要一个消化/应用周期触发你的范围,你会得到你的console.log。就这么简单。

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.

当然,原因触发摘要/申请周期可能有多个,但在我看来最终还是应该停止,如果你不触发浏览器事件或不做重载或不做某些$超时东西。如果不停止,那么你的地方搞砸了。

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.

我创建了一个Plunkr给你,让你可以检查在正常情况下,这将是一次或两次打来电话,如果你不就此采取行动,没有任何反应。如果,但是preSS按钮,这将更新一个完全不同的范围值,它会引发消化的范围/应用周期,你会得到一个额外的console.log:

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:

<一个href=\"http://plnkr.co/edit/x9I6VGP8eXtLGmT1Cuqu?p=$p$pview\">http://plnkr.co/edit/x9I6VGP8eXtLGmT1Cuqu?p=$p$pview

这篇关于AngularJS - 使用方法纳克出现连续调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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