81

I'm using the angular-ui-router library and I have a problem with URLs.

I have the following code:

app.js:

app.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
    .state('state', {
        url: '/state',
        templateUrl: 'templates/state.html',
        onEnter: function () {
            /*... code ...*/
        }
    })});

index.html:

<a href="#/state">STATE</a>

This works, but when I remove '#' from the <a> tag this doesn't work.

How can I delete the '#' sign from the URL?

2
  • 2
    I think you'll have to use HTML5 mode, otherwise using the hash for routes is needed Commented Feb 28, 2014 at 18:18
  • html5mode=true or html5mode=false Commented Feb 28, 2014 at 18:18

3 Answers 3

121

You need to enable HTML5Mode if you want navigation without hash tags:

app.config(["$locationProvider", function($locationProvider) {
  $locationProvider.html5Mode(true);
}]);

You will also need to tell angular the root URL of your app by adding the following code to the <head> of your HTML file:

<base href="/">

Be aware that support for HTML5 mode depends on the browser. For those who don't support the History API, Angular will fallback to hashbang.

Sign up to request clarification or add additional context in comments.

7 Comments

And i have another question. When I paste URL www.example.com/state, I don't have page with expected content. Can I make something with this?
It depends on your server. You need to show the same view as you would from the angular app root. AngularJs is smart enough to look at the URL and show the appropriate View / Controller. For example, if you usually go on to your app from /angular, you need to handle the case where going to /angular/state doesn't actually map to a real URL but is in fact a route in /angular and show the same view.
Also don't forget to put a <base href="/"> tag inside index.html <head> section
Or you can configure $locationProvider to not require a base tag by passing a definition object with requireBase:false to $locationProvider.html5Mode(): $locationProvider.html5Mode({ enabled: true, requireBase: false }); See: docs.angularjs.org/error/$location/nobase
@SimonBelanger i am using mvc4 and in that i have created areas for user specific models controller and view my my landing page is in Areas/Admin/View/Home/Index which uses _Layout as master template how should i write url in base href ?
|
11

If you are using Angular 1.6+, you will also need to remove the hashPrefix from the URL:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix(''); // by default '!'
  $locationProvider.html5Mode(true);
}]);

Don't forget to change the base as well:

<head>
    ...
    <base href="/">
</head>

Comments

4
    yourApp.config(function ($stateProvider, $urlRouterProvider,$locationProvider) {

    $urlRouterProvider.otherwise('/home');

    //add this line in your routing code   
    $locationProvider.html5Mode(true);

    $stateProvider.state('web.home', {
                url: '/home',
                templateUrl: 'pages/home.html',
                controller: 'mainController'         
            })
    }

in your index.php or index.html in < head > tag insert

< base href="/" >

for CodeIgniter :

<base href=" < ?php echo base_url() ?  >" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.