Is it possible to remove the # symbol from angular.js URLs?
I still want to be able to use the browser's back button, etc, when I change the view and will update the URL with params, but I don't want the # symbol.
The tutorial routeProvider is declared as follows:
angular.module('phonecat', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/phones', {templateUrl: 'partials/phone-list.html', controller: PhoneListCtrl}).
when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}).
otherwise({redirectTo: '/phones'});
}]);
Can I edit this to have the same functionality without the #?
Yes, you should configure
$locationProvider
and sethtml5Mode
totrue
:Step 1: Inject the $locationProvider service into the app config's constructor
Step 2: Add code line $locationProvider.html5Mode(true) to the app config's constructor.
Step 3: in the container (landing, master, or layout) page, add html tag such as
<base href="/">
inside the tag.Step 4: remove all '#" for routing config from all anchor tags. For examples, href="#home" becomes href="home"; href="#about" becomes herf="about"; href="#contact" becomes href="contact"
Guess this is reallllly late for this. But adding the below config to the app.module imports does the job:
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:
although the following will return the root/home page:
The reason for this is that anything after the hashtag is stripped off before the request arrives at the server. So a request for
http://foo.bar/#/portfolio
arrives at the server as a request forhttp://foo.bar
. The server will respond with a 200 OK response (presumably) forhttp://foo.bar
and the agent/client will process the rest.So in cases that you want to share a url with others, you have no option but to include the hashtag.
According to the documentation. You can use:
For example: If you click:
<a href="/other">Some URL</a>
In HTML5 Browser: angular will automatically redirect to
example.com/other
In Not HTML5 Browser: angular will automatically redirect to
example.com/#!/other
As has been mentioned in above answers the steps are-
Add to index.html Enable html5mode $location.html5Mode(true) in app.js.
With this the application will work good. But only in the scenarios when you navigate from the index.html to other pages. For example my base page is- www.javainuse.com. If I click on link java it will correctly navigate to www.javainuse.com/java.
However if i directly go to www.javainuse.com/java will get page no found error.
To resolve this we have to use url rewriting. Dot net requires URL rewriting for IIS.
If you are using tomcat then URL rewriting will have to be done using Tuckey. Can get more information on URL rewriting here