Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent redirect behaviour in angularjs app

When a user tries to click browser back button and dere is dirty data on the form, it shows a confirmation.

Here is the angular js controller code

   function MyCtrl2($rootScope, $location, $scope) {

    $rootScope.$watch(function () {
        return $location.path();
    },

    function (newValue, oldValue) {
        if ($scope.myForm.$dirty) {
            var a = confirm('do you');
            if (!a) {
                 //how to prevent this redirect
            }
        }
    },
    true);
}

MyCtrl2.$inject = ['$rootScope', '$location', '$scope'];

But how to prevent the redirect on

like image 252
agasthyan Avatar asked Dec 04 '22 12:12

agasthyan


1 Answers

FYI canceling a route is now possible in Angular.

$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
  event.preventDefault();
});
like image 173
geddski Avatar answered Dec 23 '22 23:12

geddski