javascript - AngularJs - cancel route change event -
how cancel route change event in angularjs?
my current code is
$rootscope.$on("$routechangestart", function (event, next, current) { // validation checks if(validation checks fails){ console.log("validation failed"); window.history.back(); // cancel route change , stay on current page } });
with if validation fails angular pulls next template , associated data , switches previous view/route. don't want angular pull next template & data if validation fails, ideally there should no window.history.back(). tried event.preventdefault() no use.
instead of $routechangestart
use $locationchangestart
here's discussion angularjs guys: https://github.com/angular/angular.js/issues/2109
example:
$scope.$on('$locationchangestart', function(event) { if ($scope.form.$invalid) { event.preventdefault(); } });
Comments
Post a Comment