问题描述
如何取消路由变化事件AngularJs?
How do I cancel route change event in AngularJs?
我目前的code是
$rootScope.$on("$routeChangeStart", function (event, next, current) {
// do some validation checks
if(validation checks fails){
console.log("validation failed");
window.history.back(); // Cancel Route Change and stay on current page
}
});
此即使验证失败角拉动下一个模板和相关的数据,然后立即切换回previous视图/路由。我不想角拉一个模板和放大器;数据如果验证失败,理想情况下应该没有window.history.back()。我甚至尝试事件。preventDefault(),但没有用。
with this even if the validation fails Angular pulls the next template and associated data and then immediately switches back to previous view/route. I don't want angular to pull next template & data if validation fails, ideally there should be no window.history.back(). I even tried event.preventDefault() but no use.
推荐答案
而不是 $ routeChangeStart
使用 $ locationChangeStart
下面是关于其从angularjs家伙的讨论:https://github.com/angular/angular.js/issues/2109
Here's the discussion about it from the angularjs guys: https://github.com/angular/angular.js/issues/2109
例如:
$scope.$on('$locationChangeStart', function(event) {
if ($scope.form.$invalid) {
event.preventDefault();
}
});
这篇关于AngularJs - 取消航线更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!