Skip to content

Instantly share code, notes, and snippets.

@toboqus
Last active August 29, 2015 14:06
Show Gist options
  • Save toboqus/69b2dddd9d3b07c3cf66 to your computer and use it in GitHub Desktop.
Save toboqus/69b2dddd9d3b07c3cf66 to your computer and use it in GitHub Desktop.
angular.module('Scope.safeApply', [])
.run(['$rootScope', function ($rootScope) {
$rootScope.$safeApply = function () {
var fn, phase = this.$root.$$phase;
if (arguments.length == 1) {
fn = arguments[0];
} else {
fn = arguments[1];
}
if (phase == '$apply' || phase == '$digest') {
if (fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment