Last active
August 29, 2015 14:01
-
-
Save samcrosoft/4cb1b799cf464311447f to your computer and use it in GitHub Desktop.
global safe Apply in Angular js. Stored in the $rootScope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$rootScope.safeApply(function() { | |
alert('No worries bro, you are safe'); | |
}); | |
// in controllers | |
$scope.safeApply(function(){ | |
alert("Keep Calm dude, you are safe"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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