facultymatt / angular-unsavedChanges

angular-unsavedChanges
175 stars 81 forks source link

Prevent $digest already in progress error #52

Open danielcrisp opened 9 years ago

danielcrisp commented 9 years ago

I had an edge-case issue where the confirm popup was blocking my JS thread mid-digest and I was getting an error in IE11.

It happened when clicking a button with ng-click to trigger navigation / state change.

Wrapping this in a setTimeout did the trick

    // allow any existing scope digest to complete
    setTimeout(function () {
        if (!confirm(unsavedWarningsConfig.navigateMessage)) {
            unsavedWarningsConfig.log("user wants to cancel leaving");
            event.preventDefault(); // user clicks cancel, wants to stay on page
        } else {
            unsavedWarningsConfig.log("user doesn't care about loosing stuff");
            $rootScope.$broadcast('resetResettables');
        }
    });

Also I change the scope.$apply used in the unsavedWarningForm directive for a $timeout which is safer.