$调试已经申请正在进行中 [英] Debugging $apply already in progress
问题描述
我正在开发使用AngularJS一个Chrome扩展(因此它在CSP模式下运行)。 amServices.js包含与Chrome原始消息交易的服务。因此,在JS /核心/ AM / amServices.js:268:20的相关code是如下:
I'm developing a Chrome Extension using AngularJS (so it is running in CSP mode). amServices.js contains a service that deals with Chrome native messaging. So at js/core/am/amServices.js:268:20 the relevant code is as follows:
chrome.runtime.onMessage.addListener(
function (message, sender, sendResponse) {
$rootScope.$apply(function () {
if (message.type == 'login' && message.state == 'ok') {
//huge if/else if here for every kind of message
我的理解是所有里面这里的code是越来越异步调用,并可触发在大多数应用视图,$ rootScope修改。$应用是强制性的。但是,在什么似乎完全随机的方式,我有时会在控制台中看到这些:
My understanding is that as all the code inside here is getting called asynchronously and can trigger modifications in most of the application views, $rootScope.$apply is mandatory. However, in what seems a totally random way, I sometimes get these in the console:
Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.13/$rootScope/inprog?p0=%24apply
at Error (native)
at chrome-extension://hbfchfkepmidgcdfcpnodlnmfjhekcom/lib/angular/angular.min.js:6:450
at n (chrome-extension://hbfchfkepmidgcdfcpnodlnmfjhekcom/lib/angular/angular.min.js:98:34)
at h.$apply (chrome-extension://hbfchfkepmidgcdfcpnodlnmfjhekcom/lib/angular/angular.min.js:104:195)
at chrome-extension://hbfchfkepmidgcdfcpnodlnmfjhekcom/js/core/am/amServices.js:268:20
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.dispatchToListener (extensions::event_bindings:394:22)
at Event.dispatch_ (extensions::event_bindings:378:27)
at Event.dispatch (extensions::event_bindings:400:17)
at messageListener (extensions::messaging:192:31)
没有什么错误我关于它的事实是,不像在这里解释的:
No what bugs me about it is the fact that, unlike what is explained here:
<一个href=\"https://stackoverflow.com/questions/20805534/why-doesnt-angular-ignore-subsequent-digest-invocations\">Why不角忽略随后$消化调用?
在我堆栈跟踪我不看了两眼$申请电话,所以我没有办法知道在哪里conflic从何而来。此外,我不能运行AngularJS Batarang调试工具,因为它不与CSP模式。
In my stack trace I'm not seeing two $apply calls, so I have no way to know where the conflic comes from. Additionally, I can't run AngularJS Batarang debug tool, as it doesn't work with CSP mode.
我忽略这些错误没有任何明显的后果,但我不能确定wheter它确实是安全的忽略它们。其中两个应用调用如何知道任何想法引发了冲突?
I'm ignoring these errors without any apparent consequences, but I'm unsure wheter it really is safe to ignore them. Any ideas on how to know which two apply calls triggered the conflict?
推荐答案
如果你的问题是为什么呢?和你所期望的错误只会发生,如果你有一个以上的 $适用()
If your question is why? and you are expecting the error would only happen if you had more than one $apply()
那么,作为@ J.Wells在你的问题的注解中提到的,可能是因为其他角
指令引发了 $范围。$申请
和从 chrome.runtime
而发生的角内部是在 $$阶段$ C $回调C>。
Well, as @J.Wells mentions in the comment on your question, is probably because other angular
directives triggered a $scope.$apply
and the callback from the chrome.runtime
happens while the angular internals are in a $$phase
.
看看在源code 为 NG-点击
,angularjs内部使用相同的 $范围。$适用
可用给开发者。
Take a look at the source code for ng-click
, angularjs internals use the same $scope.$apply
available to developers.
所以才考虑角度指令,如 NG-点击
, NG-变化
评估前pression使用$范围。$申请,这将启动一个 $适用
阶段,指令,如 NG-如果
和 NG-隐藏
评估使用$范围。$手表,这是一个 $消化
阶段内前pressions。
So just consider that angular directives such as ng-click
, ng-change
evaluate expression using a $scope.$apply, which starts an $apply
phase, and directives such as ng-if
and ng-hide
evaluate expressions using a $scope.$watch, which is inside a $digest
phase.
理论不好,因为作为@Wawy表明,JavaScript是单线程那么回调不能同时在摘要(左边,这样的评论有意义)执行:
的如果您点击登录按钮,用 NG-点击
指令它发生,它可能是因为铬绑定和 $消化
第一阶段的 NG-点击
完成,因此的onMessage
回调执行的
Bad theory, because as @Wawy suggests, javascript is single threaded so the callback cannot be executed while in a digest (left so that the comments make sense):
If it happens when you click a Login button, with an ng-click
directive, it could be because chrome binds and responds too fast before the $digest
phase of the ng-click
is finished, so the onMessage
callback is executed.
这篇关于$调试已经申请正在进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!