错误:$ rootScope:inprog-“不"以角度执行$ apply的正确方法是什么? [英] Error: $rootScope:inprog -- What is the correct way to 'not' execute $apply in angular?

查看:115
本文介绍了错误:$ rootScope:inprog-“不"以角度执行$ apply的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS Wiki-反模式指出以下内容:

请勿执行if (!$scope.$$phase) $scope.$apply(),这意味着您的$scope.$apply()在调用堆栈中不够高.

Don't do if (!$scope.$$phase) $scope.$apply(), it means your $scope.$apply() isn't high enough in the call stack.

我有点理解.有一个内置的逻辑确定何时需要执行什么.而且,当外部开发人员没有直接干预不同过程的运行时执行,决定在哪里运行什么程序时,它将抵消该框架.

And I kind of understand this. There is a built in logic that determines when what needs to be executed. And when an external developer, without direct insigt in the runtime executions of different procedures, determines what should run where, it counteracts the framework.

但是我当前正在上传一个大文件,通过进度条显示上传进度.我使用的库(resumableJS)依次具有一个名为fileProgress的事件,该事件在上载进行时被调用.如果在该事件期间我什么也不做,则说明栏未更新.如果将$scope.$apply()放在此处,则该栏会相应更新,但是出现摘要和/或应用已经在执行中的错误.最后,在$$phase检查的帮助下,我两全其美,获得了更新的进度条,没有任何错误.

But I am currently uploading a large file where the upload progress is displayed through a progress bar. The library I use (resumableJS) in turn has an event called fileProgress, that is called when the upload is progressing. If I do nothing during that event, the bar is not updating. If I put $scope.$apply() there, the bar is updated accordingly, but I get the digest and/or apply already in progress error. Finally, with the help of the $$phase check I get the best of both worlds, an updated progress bar without any errors.

那么在不干扰角度处理顺序的情况下更新我的钢筋的正确选择是什么?

So what is the correct alternative to get my bar updated while not interfering with the angulars processing order?

推荐答案

在您的情况下,当摘要循环已经处于同一个阶段时,您将尝试运行摘要循环.所以它抛出了一个错误

In your case you are trying to run a digest cycle, when a digest cycle is already in phase. So it's throwing an error

您可以使用诸如setTimeout(fn,0)之类的东西.它的主要作用是立即将新消息添加到队列中,并在当前执行的代码完成后进行处理.

You can use something like setTimeout(fn,0). What it basically does is it adds new message to the queue immediately, and will be processed when the currently executing code is finished.

因此,在您的角度代码中,在显式地执行$ scope.$ apply()的地方,将其更改为下面的代码即可.

So in your angular code, where you are explicitly doing your $scope.$apply(), change it to the code below and it should work.

$timeout(function(){
   $scope.$apply();
},0)

这篇关于错误:$ rootScope:inprog-“不"以角度执行$ apply的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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