我一直在寻找为Angularjs后退按钮事件实例加载,但没有一个例子已经工作了我。无论何时按下浏览器后退按钮,我的代码都不会收到事件。现在我一直在挠头。Angularjs历史后退按钮不工作
所有我想要做的就是得到一个警告,询问用户确认他们要回去和适度宽松的所有数据。
下面是我一直在努力的代码。它是来自其他例子的副本,但具有完整的代码。
我已经尝试使用$范围,而不是$ rootscope添加$上的控制器,我曾尝试在routechangestart手表,似乎没有赶上后退按钮。
是否有人有一个完整的小例子,或者告诉我什么,我做错了什么?
var BackButtonTest = angular.module('BackButtonTest', ['ngRoute']);
BackButtonTest.controller('backbuttonCtrl', function($scope, $location, $route) {
});
BackButtonTest.run(function($rootScope, $route, $location){
//Bind the $locationChangeSuccess event on the //rootScope, so that we don't need to
//bind in induvidual controllers.
$rootScope.$on('$locationChangeSuccess', function() {
$rootScope.actualLocation = $location.path();
});
$rootScope.$watch(function() {
return $location.path()
}, function (newLocation) {
if($rootScope.actualLocation === newLocation) {
// run a function or perform a reload
}
});
});
我用的HTML代码如下:
<!doctype html>
<html lang="en" ng-app="BackButtonTest">
<head>
<meta charset="utf-8">
<title>BackButtonTest App</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.min.js"></script>
<script src="app.js"></script>
<body ng-controller="backbuttonCtrl as bbc">
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-push-2">
Some random text
</div>
</div>
</div>
</body>
</html>
本文将帮助:http://weblogs.asp.net/dwahlin/cancelling-route-navigation-in-angularjs – Nix