Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created June 23, 2016 08:27
Show Gist options
  • Save tkssharma/4304d78b0760396cea90532910910e69 to your computer and use it in GitHub Desktop.
Save tkssharma/4304d78b0760396cea90532910910e69 to your computer and use it in GitHub Desktop.
$rootScope.$safeApply = function($scope, fn) {
fn = fn || function() {};
if($scope.$$phase) {
//don't worry, the value gets set and AngularJS picks up on it...
fn();
}
else {
//this will fire to tell angularjs to notice that a change has happened
//if it is outside of it's own behaviour...
$scope.$apply(fn);
}
};
//and you can run it like so.
$scope.some_value = 'value...';
$scope.$safeApply($scope, function() {
//this function is run once the apply process is running or has just finished
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment