Before reading this Article , Please check out AngularJS Routing : Part 1 Create a Single Page Application (SPA) first .
Every AngularJS Dev likes the way that the routing of AngularJS works . It uses # (Hash) in the URL like below –
But Its not very readable for Public User , non Tech people , mass people of the world , so its better to make the # invisible from the URL . And Yes, Its real EASY !!!
First Set the Html5Mode to true in LocationProvider . Now the app.js file will be like :::
$locationProvider.html5Mode(true);
$routeProvider.when("/", { templateUrl: "/partials/home.html" }).
when("/profile", { templateUrl: "/partials/profile.html" }).
when("/contact", { templateUrl: "/partials/contact.html" }).
otherwise({ redirectTo: '/' });
});
And my index.html code will be like below now :::
<td>
<a href="/">Home <i class="icon-arrow-down"></i></a>
</td>
<td>
<a href="/profile">Profile <i class="icon-arrow-down"></i></a>
</td>
<td>
<a href="/contact">Contact <i class="icon-arrow-down"></i></a>
</td>
</tr>
My SPA Site is now without that # …
Pingback: AngularJS Routing : Part 1 – Create a Single Page Application (SPA) | Shahjada Talukdar MasooM
The Code
$locationProvider.html5Mode(true);
is enable HTML5 History push method. So we can see the pages change without refresh. But there has a problem with IIS server. When we directly hit on a specific page like http://yoursite.dom/profile then the Server first see the specific url but it will show you 404 error. because the route url defination we defined only in js on client side not in server. So for SPA, server needs to point every url to main index page on load. So there should be a url rewrite machanism in server. In Apache server we can do this by htaccess.
After Page rewrite http://yoursite.dom/*anypage* will point to http://yoursite.dom/index.html and the the specific page *anypage* will load by client side angular router.
Hmmm Miraz vai , I agree and I know that , But My intention of this post was only about the client to make the # invisible .
Btw Thanks to you for this , It might help someone if he wants to know that…
Pushstate doesn’t work in <IE9 and hence the 404. works perfectly fine on chrome, firefox.
Hi, how can I do url rewriting for asp.net mvc. Entry point for my application is Index.chtml in Home Controller. Then on the top of that I am using AngularJS structure, using hot towel angular.
How to use same with $stateProvider instead of $routeProvider?
I did this but the url is still having #..
http://localhost:8080/ServletEx/index2.html#/route2
i did $locationProvider.html5Mode(true) ,,,, but it is not working
The url is looks likes this
http://localhost:8080/#%2FShowOrders
output is shown
what should i do??
sorry output is not shown.
can anyone send the complete example template to my mail for this problem?
my email—abdullahbeece@gmail.com
http://compassinhand.com/2014/01/21/building-an-angularjs-app-using-node-js-and-expressjs/
I need a complete template for this ..
This code is wrong. if you refresh url then 404 page error occur.
The code is for only Client Side. You need to do a bit on server too.Please have a look on http://compassinhand.com/2014/01/21/building-an-angularjs-app-using-node-js-and-expressjs/
How to remove # from URL when project is in TOMCAT SERVER.
We have achieved it in apache server with rewriteURL.
Plzzzz help me with tomcat server.
Hello sir
when i refresh page then 404 error in angular js + java Rest api’s.
When we refresh it goes to server and tries to check that URL . but our angularjs code in only for frontend . so u need to have a server URL route which can be pointed to root