Duplicated: Prevent error $digest already in progress when calling $scope.$apply()
That error you are getting means Angular’s dirty checking is already in progress.
Most recent best practices say that we should use $timeout if we want to execute any code in the next digest iteration:
$timeout(function() {
// the code you want to run in the next digest
});
Previous response: (don’t use this approach)
Use a safe apply, like this:
$rootScope.$$phase || $rootScope.$apply();
Why don’t you invert the condition?
$scope.$on('$locationChangeStart', function (event, next, current) {
if (confirm("Are you sure you want to leave this page?")) {
event.preventDefault();
}
});