Skip to content

Instantly share code, notes, and snippets.

@missinglink
Created November 19, 2013 18:10
Show Gist options
  • Select an option

  • Save missinglink/7549737 to your computer and use it in GitHub Desktop.

Select an option

Save missinglink/7549737 to your computer and use it in GitHub Desktop.
AngularJS SafeApply. This fixes the annoying angular error: "Error: $digest already in progress"
app.factory( 'SafeApply', function() {
return function($scope, fn) {
var phase = $scope.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
$scope.$apply(fn);
}
}
});
@missinglink
Copy link
Copy Markdown
Author

Usage:

SafeApply( $scope, function(){
  $scope.cats = 'awesome';
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment