Skip to content

Instantly share code, notes, and snippets.

@samcrosoft
Last active August 29, 2015 14:01
Show Gist options
  • Save samcrosoft/4cb1b799cf464311447f to your computer and use it in GitHub Desktop.
Save samcrosoft/4cb1b799cf464311447f to your computer and use it in GitHub Desktop.
global safe Apply in Angular js. Stored in the $rootScope
$rootScope.safeApply(function() {
alert('No worries bro, you are safe');
});
// in controllers
$scope.safeApply(function(){
alert("Keep Calm dude, you are safe");
});
$rootScope.safeApply = function(oEventCallback) {
var sCurrentPhase = this.$root.$$phase;
var aPhases = ['$apply', '$digest'];
if(aPhases.indexOf(sCurrentPhase) < 0)
{
this.$apply(oeventCallback);
}
else
{
if(fn && (typeof oEventCallback === 'function'))
{
return oEventCallback();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment