htmlangularjspreventdefaultui-sref

AngularJs - My ui-sref anchor tag can't be cancelled by event.preventDefault()


I have this tag:

<a ui-sref="MyModule">My Module</a>

When I click on the link, this will be executed:

$scope.$on("$locationChangeStart", function (event) {

        if (!confirm('You have unsaved changes, continue?')) {
            event.preventDefault();
        }
    });

But still my view will switch to MyModule interface.


Solution

  • try using this:-

    $scope.$on("$stateChangeStart", function (event) {
    
            if (!confirm('You have unsaved changes, continue?')) {
                event.preventDefault();
            }
        });
    

    You are using UI-router. which is state based.