Skip to content

Instantly share code, notes, and snippets.

@funyx
Forked from siongui/gist:4969449
Last active November 8, 2015 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funyx/217275efdde5bda83f43 to your computer and use it in GitHub Desktop.
Save funyx/217275efdde5bda83f43 to your computer and use it in GitHub Desktop.
AngularJS safe $apply (prevent "Error: $apply already in progress")
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest')
this.$eval(fn);
else
this.$apply(fn);
};
// OR
function safeApply(scope, fn) {
var phase = scope.$root.$$phase;
if(phase == '$apply' || phase == '$digest')
scope.$eval(fn);
else
scope.$apply(fn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment