Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Allow scope.$eval even while a $digest is in progress. #1618

Closed
inklesspen opened this issue Nov 28, 2012 · 4 comments
Closed

Allow scope.$eval even while a $digest is in progress. #1618

inklesspen opened this issue Nov 28, 2012 · 4 comments

Comments

@inklesspen
Copy link

I am integrating some jQuery functionality into my Angular app, which involves the use of jQuery callbacks. The docs indicate that callbacks executing outside of the context of a scope should use scope.$eval to make sure everything gets updated. This does indeed work. However, sometimes my callbacks are called immediately rather than later on, which means I get a "$digest already in progress" error.

I think I can work around this by wrapping all my callbacks in a setTimeout, but this seems inelegant. Perhaps there should be some way to let a callback which may or may not be executed outside a scope to wrap itself in the scope?

@petebacondarwin
Copy link
Contributor

There has been discussion about this before. The standard work around is
to use $timeout (not setTimeout), with no (or a zero) delay). Another
alternative if you are only using $scope.$eval, could be to try
$scope.$evalAsync.

On 28 November 2012 04:18, Jon Rosebaugh notifications@github.com wrote:

I am integrating some jQuery functionality into my Angular app, which
involves the use of jQuery callbacks. The docs indicate that callbacks
executing outside of the context of a scope should use scope.$eval to make
sure everything gets updated. This does indeed work. However, sometimes my
callbacks are called immediately rather than later on, which means I get a
"$digest already in progress" error.

I think I can work around this by wrapping all my callbacks in a
setTimeout, but this seems inelegant. Perhaps there should be some way to
let a callback which may or may not be executed outside a scope to wrap
itself in the scope?


Reply to this email directly or view it on GitHubhttps://github.com//issues/1618.

@benlesh
Copy link
Contributor

benlesh commented Nov 30, 2012

@petebacondarwin Couldn't a solution for this be to test to see if it's in a $digest in $eval, then set it to call in $timeout?

Something like (psuedo code):

function $eval(fn, x) {
   if(isDigesting) {
       x = x || 0;
       if ( x++ > 10 ) {
            throw new Error('too many tries');
       }
       $timeout( function() { $eval( fn, x ) }, 0 );
   }
}

@petebacondarwin
Copy link
Contributor

Actually, I think you need to rethink this issue. $eval is designed to be called from within $digest. Perhaps you mean calling $apply or $digest from within a $digest?
See this http://plnkr.co/edit/976XAW?p=preview

@inklesspen
Copy link
Author

Hm, I'm pretty sure I was having the error with $eval. I will attempt to isolate a plunker.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants