消化周期叫什么名字

消化周期叫什么名字

本文介绍了当$消化周期叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当摘要周期正在发生的事情很困惑,是不是定期调用基于定时器每50ms(因为它说的并暗示)或它调用之后每次进入角范围内(因为它是事件说的,here和here) ?

I'm very confused when a digest cycle is happening, is it called periodically based on a timer every 50ms (as it's says here and implied here) or is it called after every event that enters the angular context (as it's says here, here and here) ?

例如,当它是物质:

在我的模型我有一个名为 myVar的以3变量的值。
在我的HTML我有 {{MYVAR}}
作为一个按钮单击事件,例如被激发并提高控制器的处理程序,处理程序里面的code是:

In my model I have a variable called myVar with the value of 3.In my html I have {{myvar}}.An event such as a button click is fired and raises a handler in the controller, the code inside the handler is:

$scope.myVar = 4;
// some heavy actions takes place for 3 seconds...
$scope.myVar = 5;

假设UI线程不堵塞,将会在用户看到什么按钮,点击后?将他看到的只有5或他会看到4和后3秒5?

Assuming the UI thread is not blocked, what will the user see after the button click? will he see only 5 or will he see 4 and after 3 seconds 5?

推荐答案

我觉得消化周期在描述http://blog.bguiz.com/post/60397801810/digest-cycles-in-single-page-apps这是

I think the description of the digest cycle at http://blog.bguiz.com/post/60397801810/digest-cycles-in-single-page-apps that it is

这是运行在一个区间code

是非常误导,而且说实话,指的是角的时候,我甚至会说的不对。引用帕维尔·科兹洛夫斯基,<一个href=\"http://books.google.co.uk/books?id=mZXjwz5X08EC&pg=PT547&lpg=PT547&dq=angularjs%20does%20not%20use%20any%20kind%20of%20polling%20mechanism%20to%20periodically%20check%20for%20model%20changes&source=bl&ots=T4H4PSXaST&sig=qR-usD2QqriowWkSNmkRC7EI1Rk&hl=en&sa=X&ei=4Y1mU_uCDYPBO4LkgJAH&ved=0CDEQ6AEwAA#v=onepage&q=angularjs%20does%20not%20use%20any%20kind%20of%20polling%20mechanism%20to%20periodically%20check%20for%20model%20changes&f=false\">Mastering Web应用程序开发与AngularJS

is very misleading, and to be honest, when referring to Angular, I would even say wrong. To quote Pawel Kozlowski, Mastering Web Application Development with AngularJS

AngularJS不使用任何形式的投票机制,定期检查模型更改

要证明没有投票,如果你有一个模板

To prove there is no polling, if you have a template of

<p>{{state}}</p>

和控制器code

$scope.state = 'Initial';
// Deliberately *not* using $timeout here
$window.setTimeout(function() {
  $scope.state = 'Changed';
},1000);

时,则向用户显示该字符串仍将作为初始永不更改为更改

as in this plunker, then the string shown to the user will remain as Initial and never change to Changed.

如果你想知道为什么你经常看到 $调用适用,但并非总是如此,可能是因为来与棱角分明各种指令,如 ngClick ngChange 将调用 $适用自己,这之后会触发的周期。事件监听器到本机JS事件直接将无法做到这一点,所以他们会刻意叫 $适用来所做的任何更改反映在模板中。

If you're wondering why you often see calls to $apply, but not always, it is probably because the various directives that come with Angular, such as ngClick or ngChange will call $apply themselves, which will then trigger the cycle. Event listeners to native JS events directly will not do this, so they will have to deliberately call $apply to have any changes made reflected in templates.

这篇关于当$消化周期叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:31