0

Currently I'm using Angular Js routing this way and it's working fine.

config(["$routeProvider", function ($routeProvider, $mdThemingProvider) {
        $routeProvider
            .when('/', {
                templateUrl: '/templates/home.html',
                controller: 'smu72Controller'
            })
       .when('/contact', {
           templateUrl: '/templates/contact.html',
           controller: 'smu72Controller'
       })
            .when('/objects', {
                templateUrl: '/templates/objects.html',
                controller: 'smu72Controller'
            })
            .when('/objects/:objectId', {
                templateUrl: '/templates/object.html',
                controller: 'smu72Controller'
            })
            .when('/services', {
                templateUrl: '/templates/services.html',
                controller: 'smu72Controller'
            })
        .otherwise({
            redirectTo: "/"
        });
    }
    ]);

But with this code I got ugly looking and not seo friendly urls as host/index.html#sectionname, how should I change this to get route url as host/sectionname?

P.S. I'm getting templates to div with ng-view on Index.html page.

1
  • Thanks you guys, both answers are correct actually, so I just select oldest one. Commented Apr 11, 2016 at 9:47

2 Answers 2

1

by using the $locationProvider module and setting html5Mode to true (the use of HTML5 History API) we get rid of the ugly #

config(function($routeProvider, $locationProvider) {
    $routeProvider
            .when('/', {
                 templateUrl: '/templates/home.html',
                 controller: 'smu72Controller'
             })
    /* your all other route specifications */

    $locationProvider.html5Mode(true);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Dont just dump code, try to add some description as well
@dreamweiver, you're right, but this answer clear enough.
1

By default Angular uses something called hashbang mode, you can turn this off and use HTML5 mode - with this you get nice urls - but it won't work in old browsers. To turn this on, put the line below in the config section of your app - remeber to inject $locationProvider.

$locationProvider.html5Mode(true);

Read more here https://docs.angularjs.org/guide/$location .

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.