簡體   English   中英

在瀏覽器中更改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>

在瀏覽器窗口中:

  1. 1234567更改為1234
  2. 點擊Enter

什么都沒發生。 按F5,它按預期工作。

不知道這是否與瀏覽器行為有關,但是如何在參數更改+ Enter之后強制頁面刷新?

頁面刷新已在此處回答

您可以使用$route.reload();

如此處的建議當在url輸入中按Enter時您可以嘗試偵聽路由更改成功:

scope.$on('$routeChangeSuccess')scope.$on('$locationChangeSuccess').

基於上述萊昂建議的完整示例

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM