Last active
April 7, 2020 08:49
-
-
Save siongui/4969449 to your computer and use it in GitHub Desktop.
AngularJS safe $apply (prevent "Error: $apply already in progress")
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') | |
this.$eval(fn); | |
else | |
this.$apply(fn); | |
}; | |
// OR | |
function safeApply(scope, fn) { | |
var phase = scope.$root.$$phase; | |
if(phase == '$apply' || phase == '$digest') | |
scope.$eval(fn); | |
else | |
scope.$apply(fn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have issue
somethims this.$root.$$phase is undefined
change like this?