sam
sam

Reputation: 427

locationProvider not working for angular

While working on a course from Pluralsight , I am having a problem.

angularFormsApp.config(
    ["$routeProvider", "$locationProvider",
    function ($routeProvider, $locationProvider) {
     $routeProvider
    .when("/home", {
        templateUrl: "app/Home.html",
        controller: "HomeController"
    })
    .when("/newEmployee", {
        templateUrl: "app/Employee/employeeTemplate.html",
        controller: "employeeController"
    })
    .when("/updateEmployee/:id", {
            templateUrl: "app/Employee/employeeTemplate.html",
            controller: "employeeController"
    })
    .otherwise({
        redirectTo: "/home"
    });
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
}]);

I had to add 'requireBase' after looking into a forum as it was giving error without using this. Now if run my code, I can't go to Update Employee page directly. When I clicked Update Employee, nothing happened, i pressed cancel and then 'Add Employee' but the 'Update Employee' became visible instead of 'Add'. When I clicked submit, it gave me 'Not Found' server side error without entering debugging code.

I commented the code

  $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

to see the urls being passed and then it worked. Can you please help me on this as practically we have to use this code to hide the urls.

Upvotes: 0

Views: 122

Answers (1)

sam
sam

Reputation: 427

I have finally found an answer. The shortcut to this method is:

$locationProvider.html5Mode(true);

This will be equal to

$locationProvider.html5Mode({
    enabled: true,
    requireBase: true,
    rewriteLinks: true
});

What I was doing wrong was when I used the shortcut, my f12 tools in the browser were giving the following error: "locationProvider require a base href tag" I confused this with the requireBase parameter in the above statement and nothing worked for me. I added the following:

 <base href="/" />

in the head section of my Index.cshtml page and it worked.

Upvotes: 1

Related Questions