![](/img/trans.png)
[英]Refresh browser page on browser back click when changing hash value in url
[英]Angular page refresh on changing URL parameter in browser
極其簡單的要求,但是很難。 有一個簡單的頁面,我在其中在url路徑中打印參數。
http://localhost:8888/ngtest.html#/1234567
ngtest.html
<div ng-app="app" ng-controller="testCtrl">
{{myId}}
</div>
<script>
var app = angular.module('app', []);
app.controller('testCtrl', function($scope, $location) {
$scope.myId = $location.$$path;
});
</script>
在瀏覽器窗口中:
1234567
更改為1234
什么都沒發生。 按F5,它按預期工作。
不知道這是否與瀏覽器行為有關,但是如何在參數更改+ Enter之后強制頁面刷新?
基於上述萊昂建議的完整示例
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
<div ng-app="app" ng-controller="testCtrl">
{{myId}}
</div>
<script>
var app = angular.module('app', []);
app.controller('testCtrl', function($scope, $location) {
// add a listener for when the url changes and user
// Enter in the address bar
$scope.$on('$locationChangeStart', function(event) {
$scope.myId = $location.$$path;
});
});
</script>
基本上,這是注冊一個位置更改偵聽器並相應地執行操作。 無需路由配置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.