Skip to content

Instantly share code, notes, and snippets.

@egfx
Created November 6, 2013 00:26
Show Gist options
  • Save egfx/7328810 to your computer and use it in GitHub Desktop.
Save egfx/7328810 to your computer and use it in GitHub Desktop.
Angular Safe Apply - https://coderwall.com/p/ngisma
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
//And then just replace $apply with safeApply wherever you need it
$scope.safeApply(function() {
alert('Now I'm wrapped for protection!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment