In my AngularJS application, I have a page with several subsections. All of these subsections are on the same page and are part of the same template. However, I want to access each section through its own URL, which will scroll to the desired section if it matches.
I set the states like this:
.state('about', { url: "/about", templateUrl: "partials/about.html", }) .state('about.team', { url: "/about/team", templateUrl: "partials/about.html" }) .state('about.office', { url: "/about/office", templateUrl: "partials/about.html" }) .state('about.other', { url: "/about/other", templateUrl: "partials/about.html" });
And on the about page, I have a menu like:
<div class="menu"> <a ui-sref-active="selected" ui-sref="about.team">The Team</a> <a ui-sref-active="selected" ui-sref="about.office">The Office</a> <a ui-sref-active="selected" ui-sref="about.other">Other</a> </div>
This menu is fixed at the bottom, and when the user clicks on the link or when visiting the URL directly through the address bar, he should scroll to this section (updating the URL where necessary for compliance).
However, I'm not sure how to do this (via Angular). Summarizing:
- How to scroll pages on one page using url? Since I cannot use the standard hash trick, since it already uses hashes for the address (we support IE8). And I don't want to use hashes at all when in HTML5 mode! It should also go to the download section of the AFTER application download, so you need to somehow run the code that scrolls the user after something else ...
- How can I do this work by pressing buttons? Currently, when you click on the buttons, they change the URL, but do not scroll to the content.
The HTML for the page looks like: http://pastebin.com/Q60FWtL7
Here you can see the application: http://dev.driz.co.uk/AngularPage/app/#/about
An example of something similar ( https://devart.withgoogle.com/#/about/your-opportunity ), since you can see that it scrolls to the desired section based on url AFTER the page loads ... and does not use a hash, except for the ACTUAL URL.
javascript angularjs angular-ui-router
Cameron
source share