简体   繁体   English

的摘要循环异步吗?

[英]is angular's digest cycle asynchronous?

I have the following piece of code: 我有以下代码:

$scope.$apply(function () {
    $scope.$emit("connectionadd.flowchart", data);
});

if (data.cancel) {
    return false;
} else {
    return true;
}

I'm wondering whether the execution can reach if (data.cancel) line before everything inside $scope.$apply callback is executed? 我想知道在$scope.$apply回调内部的所有内容执行之前,执行是否可以到达if (data.cancel)行? If $digest is async, than it's possible. 如果$digest是异步的,则有可能。

No its not, pseudo code: 不,不是,伪代码:

function $apply(expr) {
  try {
    return $eval(expr);
  } catch (e) {
    $exceptionHandler(e);
  } finally {
    $root.$digest();
  }
}

So the answer is that it will not reach if (data.cancel) before completing, although it is possible to make that happen with $scope.$applyAsyncc 因此答案是, if (data.cancel)在完成之前也不会到达,尽管可以使用$scope.$applyAsyncc做到这一点$scope.$applyAsyncc

$digest is also not async, but even it was purely by answering your question, then callback still would be executed before, because digest is called after the callback execution. $ digest也不是异步的,但是即使是纯粹通过回答您的问题,回调也仍然会在执行之前执行,因为摘要是在回调执行之后调用的。

You can review $digest code here: https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js 您可以在此处查看$ digest代码: https//github.com/angular/angular.js/blob/master/src/ng/rootScope.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM