2015-05-18 97 views
0

非常簡單的要求,但這很困難。有簡單的頁面,我在URL路徑中打印參數。在瀏覽器中更改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. 命中進入

沒有任何反應。命中F5,並按預期工作。

不確定這是否與瀏覽器行爲有關,但如何在參數更改後強制頁面刷新+ Enter?

+0

您可能需要爲此設置路由功能。請參閱:https://www.youtube.com/watch?v=TRrL5j3MIvo&feature=youtu.be&t=58m11s – isherwood

+0

爲什麼要刷新? SPA的重點在於避免這一點。 – isherwood

+0

絕對沒問題,如果它不起作用。基本上我有入站鏈接,如'http://example.com/view.html#/ {item_number}'。試圖看看它是否會在沒有路線的情況下運作。 – Fakeer

回答

1

刷新頁面已經回答了here.

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

至於建議here,你可以嘗試打請在網址輸入時監聽線路變化的成敗:

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

+0

找出下面註冊的完整代碼。路由重新加載是不需要的。 – Fakeer

1

基於Leon上述建議的完整示例

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

基本位置這是註冊一個位置更改偵聽器並相應地做事情。無需路由配置