AngularJS routing url remove this #!
You have to understand that Angular is not a server side process. You can tweak the html5mode but that is only functional for links included in html anchors of your page and how the url looks like in the browser address bar. Attempting to request a subpage without the hashtag (with or without html5mode) from anywhere outside the page will result in a 404 error. For example, the following CURL request will result in a page not found error, irrespective of html5mode:
$ curl http://foo.bar/portfolio
although the following will return the root/home page:
$ curl http://foo.bar/#/portfolio
So in cases that you want to share a url with others, you have no option but to include the hashtag; unless you resolve to some type of server side routing.
How to handle anchor hash linking in AngularJS
You're looking for $anchorScroll()
.
Here's the (crappy) documentation.
And here's the source.
Basically you just inject it and call it in your controller, and it will scroll you to any element with the id found in $location.hash()
app.controller('TestCtrl', function($scope, $location, $anchorScroll) {
$scope.scrollTo = function(id) {
$location.hash(id);
$anchorScroll();
}
});
<a ng-click="scrollTo('foo')">Foo</a>
<div id="foo">Here you are</div>
Here is a plunker to demonstrate
EDIT: to use this with routing
Set up your angular routing as usual, then just add the following code.
app.run(function($rootScope, $location, $anchorScroll, $routeParams) {
//when the route is changed scroll to the proper element.
$rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
$location.hash($routeParams.scrollTo);
$anchorScroll();
});
});
and your link would look like this:
<a href="#/test?scrollTo=foo">Test/Foo</a>
Here is a Plunker demonstrating scrolling with routing and $anchorScroll
And even simpler:
app.run(function($rootScope, $location, $anchorScroll) {
//when the route is changed scroll to the proper element.
$rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
if($location.hash()) $anchorScroll();
});
});
and your link would look like this:
<a href="#/test#foo">Test/Foo</a>
Change the URL breaks the routing prevent
You lose angular state when you go to the url of the root rather than the state url (i.e) #(hash) urls. The root url reloads the page wherein you lose memory of all javascript variables as they are all client side. Hence the variable is undefined.
State changes happen in a single instance of page load, the url changes give you a illusion as if a page load is happening
Angular: how to handle both HTML5 mode routes and hash routes?
Use $routeChangeStart :
angular.module('routing', ['ngRoute'])
.run(['$rootScope', '$location', '$window', function($rootScope, $location, $window) {
$rootScope.$on("$routeChangeStart",
(event, current, previous, rejection) => {
if (/#\//.test($window.location.hash)) {
$location.path($window.location.hash.replace('#', ''));
}
});
...
AngularJS ui-router: ui-sref links aren't prepending the hash when filling in href values
I added this to my main app.js
as a workaround:
.run(['$state',
function($state) {
$state.originalHrefFunction = $state.href;
$state.href = function href(stateOrName, params, options) {
var result = $state.originalHrefFunction(stateOrName, params, options);
if (result && result.slice(0, 1) === '/') {
return '#' + result;
} else {
return result;
}
}
}
])
Related Topics
Enabling Cross-Origin Resource Sharing on Iis7
What Benefits Does Es2015 (Es6) 'Class' Syntax Provide
How to Tell If Caps Lock Is on Using JavaScript
How to Change Href of <A> Tag on Button Click Through JavaScript
Why Is 'Replace' Property Deprecated in Angularjs Directives
Get Unique Selector of Element in Jquery
Want HTML Form Submit to Do Nothing
How to Detect Element Being Added/Removed from Dom Element
Anchor Jumping by Using JavaScript
What Is the Minimum Valid JSON
Verify If a Point Is Land or Water in Google Maps
How to Implement Authenticated Routes in React Router 4
JavaScript Require() Function Giving Referenceerror: Require Is Not Defined
Sending Emails with JavaScript
Is This an Example of Variable Shadowing in JavaScript
How to Use JavaScript to Read Local Text File and Read Line by Line