我在Angular JS上度過了我的第一個幾個小時,我正在試着寫一個SPA。然而,在改變路線時,在改變路線之後滾動位置保持在其當前位置。這意味着如果有人通讀了第二頁上的一半文字,這個人在轉換到第二頁之後將會在頁面中間結束。 (假設頁面長度相同)如何重置路線更改的滾動位置?
當我尋找解決方案時,我只找到人們詢問相反的內容,即他們在更換頁面後不想更改滾動位置。但是,我甚至沒有重現。我不知道Angular JS的發展是否超前,我諮詢的來源是否過時。
我創建了一個最小版本的演示我的問題(只需添加兩個文件sample1.html
和sample2.html
隨機內容,使其工作。):
<!DOCTYPE html>
<html>
<head lang="en"><title>SPA sample</title></head>
<body data-ng-app="myApp">
<div style="position: fixed;">
<a href="#/">Main</a>
<a href="#/other">Tutorial</a>
</div>
<div data-ng-view=""></div>
<script src="http://code.angularjs.org/1.2.9/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.9/angular-route.min.js"></script>
<script>
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'noOp',
templateUrl: 'sample1.html'
})
.when('/other', {
controller: 'noOp',
templateUrl: 'sample2.html'
})
.otherwise({redirectTo: '/'});
});
myApp.controller('noOp', function ($scope) { });
</script>
</body>
</html>
你可能會尋找[$ anchorScroll](http://docs.angularjs.org/ API/NG /服務/ $ anchorScroll)。 – miqid
我通讀了它。那麼,現在是noop的控制器應該獲得這個錨點滾動並應用它?對不起,我很新的角度。 –