This tutorial is out of date and no longer maintained.
By default, AngularJS will route URLs with a hashtag.
For example:
http://example.com/
http://example.com/#/about
http://example.com/#/contact
It is very easy to get clean URLs and remove the hashtag from the URL.
There are 2 things that need to be done.
$locationProvider
In Angular, the $location
service parses the URL in the address bar and makes changes to your application and vice versa.
I would highly recommend reading through the official Angular $location
docs to get a feel for the $location
service and what it provides.
We will use the $locationProvider
module and set html5Mode
to true
.
We will do this when defining your Angular application and configuring your routes.
angular.module('scotchy', [])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : 'partials/home.html',
controller : mainController
})
.when('/about', {
templateUrl : 'partials/about.html',
controller : mainController
})
.when('/contact', {
templateUrl : 'partials/contact.html',
controller : mainController
});
// use the HTML5 History API
$locationProvider.html5Mode(true);
});
What is the HTML5 History API? It is a standardized way to manipulate the browser history using a script. This lets Angular change the routing and URLs of our pages without refreshing the page. For more information on this, here is a good HTML5 History API Article.
To link around your application using relative links, you will need to set a <base>
in the <head>
of your document.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<base href="/">
</head>
There are plenty of other ways to configure this and the HTML5 mode set to true should automatically resolve relative links. This has just always worked for me. If the root of your application is different than the URL (for instance /my-base
), then use that as your base.
The $location
service will automatically fallback to the hashbang method for browsers that do not support the HTML5 History API.
This happens transparently to you and you won’t have to configure anything for it to work. From the Angular $location
docs, you can see the fallback method and how it works.
This is a simple way to get pretty URLs and remove the hashtag in your Angular application. Have fun making those super clean and super fast Angular apps!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!