84

I am trying to implement html5's pushstate instead of the # navigation used by Angularjs. I have tried searching google for an answer and also tried the angular irc chat room with no luck yet.

This is my controllers.js:

function PhoneListCtrl($scope, $http) {
    $http.get('phones/phones.json').success(function(data) {
        $scope.phones = data;
    });
}

function PhoneDetailCtrl($scope, $routeParams) {
  $scope.phoneId = $routeParams.phoneId;
}

function greetCntr($scope, $window) {
    $scope.greet = function() {
    $("#modal").slideDown();
    }
}

app.js

angular.module('phoneapp', []).
    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'
            });
    }])

1 Answer 1

129

Inject $locationProvider into your config, and set $locationProvider.html5Mode(true).

http://docs.angularjs.org/api/ng.$locationProvider

Simple example:

JS:

myApp.config(function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider
    .when('/page1', { template: 'page1.html', controller: 'Page1Ctrl' })
    .when('/page2', { template: 'page2.html', controller: 'Page2Ctrl' })
});

HTML:

<a href="/page1">Page 1</a> | <a href="/page2">Page 2</a>
Sign up to request clarification or add additional context in comments.

15 Comments

Could I see a code sample for this when there's already config() for routeProvider? Not sure if I'm supposed to be creating a new config() for this or adding it to the first as an array of configs, and also not sure what the configFn should be (docs.angularjs.org/api/angular.module)
I have done the same but when i click /phones/:phoneId the template url become phones/partials/phone-detail.html and firebug net tab shows html page not found
I am also getting page not found whenever I navigate to a given url in the browser (e.g. /home instead of /). Have you tried enabling html5 for your own site?
You have to configure your server to allow you to pull pages correctly. When you try to navigate to mysite.com/hello, it is trying to GET that page from your server. Instead, you want the browser to GET mysite.com and then use angular to find the browser's location is /hello and go there. So you need to configure your server to give back the mysite.com data no matter which subdomain is entered.
I'd say put your rest API at /api or something, and then make everything but /api/* give out the index.html.
|

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.