我正在尝试使用AngularJS创建一个应用程序。目前,我的应用中只有一个视图。我的应用会有多个视图。目前,我的index.html文件如下所示:
的index.html ...
Choose:
<a href="/view-1">View 1</a> |
<a href="/view-2">View 2</a>
<div class="view-animate-container">
<div id="driver" ng-view class="view-animate" style="margin: 0; padding: 0; height: 100%;"></div>
</div>
<hr />
</div>
</body>
view1.html
<div>
<div style="position:fixed;">
<h1>View 3</h1>
<div>
<a href="#sectionA">Section A</a> | <a href="#sectionB">Section B</a> | <a href="#sectionC">Section C</a>
</div>
</div><br /><br /><br /><br />
<div style="overflow-y:scroll">
<h2 id="sectionA">Section A</h2>
<div style="background-color:navy; color:white;">
Some text content goes here.
<br /><br /><br /><br /><br />
<br /><br /><br /><br /><br />
Yup.
</div>
<h2 id="sectionB">Section B</h2>
<div style="background-color:red; color:white;">
Some text content goes here.
<br /><br /><br /><br /><br />
<br /><br /><br /><br /><br />
Yup.
</div>
<h2 id="sectionC">Section C</h2>
<div style="background-color:peachpuff; color:black;">
Some text content goes here.
<br /><br /><br /><br /><br />
<br /><br /><br /><br /><br />
Yup.
</div>
</div>
</div>
main.js
angular.module('myApp', ['ngRoute', 'ngAnimate'])
.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/view-1', {
templateUrl: '/views/view1.html',
controller: View1Ctrl
});
$routeProvider.when('/view-2', {
templateUrl: '/views/view2.html',
controller: View2Ctrl
});
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
})
;
function View1Ctrl($scope, $routeParams) {
this.name = "View1Cntl";
this.params = $routeParams;
}
function View2Ctrl($scope, $routeParams) {
this.name = "View2Cntl";
this.params = $routeParams;
}
我正试图让我的书签(锚标签)在view1中工作。每当我点击“A部分”,“部分B”或“部分C”时,路线都会更新。然后视图重新动画到屏幕上。最后,视图定位到相应的部分。所以问题是视图重新加载。实际上,我只想在有人点击它时跳转到页面中的书签。我如何在AngularJS中执行此操作?
谢谢!
答案 0 :(得分:4)
我能够使用scrollTo
函数完成类似的行为,并将href
替换为调用它的ng-click
标记。
<强> main.js 强>
$scope.scrollTo = function(selector) {
window.scrollTo(0, $(selector)[0].offsetTop - 100);
}
<强> view1.html 强>
<div>
<a ng-click="scrollTo('#sectionA')">Section A</a> |
<a ng-click="scrollTo('#sectionB')">Section B</a> |
<a ng-click="scrollTo('#sectionC')">Section C</a>
</div>
<h2 id="sectionA">Section A</h2>
<div style="background-color:navy; color:white;">
Some text content goes here.
</div>
<h2 id="sectionB">Section B</h2>
<div style="background-color:navy; color:white;">
Some text content goes here.
</div>
<h2 id="sectionC">Section C</h2>
<div style="background-color:navy; color:white;">
Some text content goes here.
</div>
答案 1 :(得分:1)
我得到了最好和最简单的解决方案:
将href="#bottom"
链接替换/更改为/#/dashboard#bottom
/#/dashboard
是我(当前)/想要滚动的页面,#bottom
是锚链接。
我有标签被翻转并使用上述方法我无法按照我想要的方式解决问题。借着上帝的恩典,我得到了一个小小的hacky解决方案:)