3 回答

TA貢獻1827條經驗 獲得超8個贊
無需更新路徑,只需使用頁碼更新查詢參數。
設置路由以忽略查詢參數更改:
....
$routeProvider.when('/foo', {..., reloadOnSearch: false})
....
并在您的應用程序中更新$location:
...
$location.search('page', pageNumber);
...

TA貢獻1946條經驗 獲得超4個贊
從這篇博客文章:
默認情況下,所有位置更改都會經過路由過程,該過程會更新角度視圖。
但是,有一種簡單的方法可以將其短路。Angular監視位置更改(是否通過輸入位置欄,單擊鏈接或通過來設置位置來完成 $location.path())。當感知到此更改時,它將廣播事件$locationChangeSuccess,并開始路由過程。我們要做的是捕獲事件并將路由重置為以前的狀態。
function MyCtrl($route, $scope) {
var lastRoute = $route.current;
$scope.$on('$locationChangeSuccess', function(event) {
$route.current = lastRoute;
});
}

TA貢獻2080條經驗 獲得超4個贊
我的解決方案是使用$ routeChangeStart,因為它為您提供了“下一條”和“最后一條”路線,您可以比較它們而無需像$ locationChangeSuccess那樣的額外變量。
好處是能夠訪問“下一條”路徑和“最后一條”路徑上的“ params”屬性,例如next.params.yourproperty當您使用“ / property / value” URL樣式時,當然$location.url還是使用或$location.path更改URL,$location.search()這取決于“?property = value” URL樣式。
在我的情況下,我不僅將它用于此目的,而且還防止控制器未更改而更改路由:
$scope.$on('$routeChangeStart',function(e,next,last){
if(next.$$route.controller === last.$$route.controller){
e.preventDefault();
$route.current = last.$$route;
//do whatever you want in here!
}
});
我個人覺得AngularJS應該提供一種控制它的方法,現在他們認為,每當您更改瀏覽器的位置時,您都希望更改路線。
- 3 回答
- 0 關注
- 1045 瀏覽
添加回答
舉報