Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Last active August 29, 2015 14:21
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 chrisyip/bb20721b1ad3f2a42e17 to your computer and use it in GitHub Desktop.
Save chrisyip/bb20721b1ad3f2a42e17 to your computer and use it in GitHub Desktop.
Safer $apply for angular, suppress $digest already in progress error
angular.module('myApp', [])
.run(function ($rootScope, $exceptionHandler) {
$rootScope.$safeApply = function (exp) {
var phase = (this.$root || this).$$phase
if (phase !== '$apply' && phase !== '$digest') {
this.$apply(exp)
} else {
try {
this.$eval(exp)
} catch (ex) {
$exceptionHandler(ex)
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment