$调试已经申请正在进行中 [英] Debugging $apply already in progress

查看:140
本文介绍了$调试已经申请正在进行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用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 而发生的角内部是在 $$阶段

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屋!

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