我得到了一个基于URL更改来处理html视图的代码示例。除Browser Back/Forward button
以外,其他一切正常。
我正在使用Angular路由(ngRoute)。在特定的路由上,我将reloadOnSearch:false
设置为避免控制器重新加载为:
app.config(function($routeProvider,$locationProvider,$httpProvider,
$mdDateLocaleProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/trans',{
templateUrl: 'user_trans.html',
authenticated: true,
controller: 'dashCtrl',
reloadOnSearch: false
})
}
app.run(function($rootScope,$location,userModel,$window,$routeParams,$route){
$rootScope.$on('$routeUpdate', function(event, next, current){
$rootScope.actualLocation = $location.path();
if (next.$$route.authenticated) {
if (!userModel.getAuthStatus()) {
$location.url('/').replace(undefined);
}
}
if (next.$$route.originalPath =='/') {
if (userModel.getAuthStatus()) {
next.$$route.originalPath = '/home';
$location.path(next.$$route.originalPath);
$location.url('/home').replace(undefined);
}
else {
}
}
//TO detect browser back button:
$rootScope.$watch(function () {return $location.path()}, function (newLocation, oldLocation) {
console.log("checking old and new location")
if($rootScope.actualLocation === newLocation) {
alert('Why did you use history back?');
}
});
})
这里提到to detect browser back/forward button
,当我执行浏览器后退前进按钮时,该代码块未访问。