Created
November 6, 2013 00:26
-
-
Save egfx/7328810 to your computer and use it in GitHub Desktop.
Angular Safe Apply - https://coderwall.com/p/ngisma
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
$scope.safeApply = function(fn) { | |
var phase = this.$root.$$phase; | |
if(phase == '$apply' || phase == '$digest') { | |
if(fn && (typeof(fn) === 'function')) { | |
fn(); | |
} | |
} else { | |
this.$apply(fn); | |
} | |
}; |
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
//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