Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs auto prefixes forward slash

If I hit the url say

www.xyz.com/home#route-1

AngularJS automatically re-directs it to

www.xyz.com/home#/route-1

That is - it prefixes the route with a / (forward slash)

Why is it happening and how can I stop making it do this?

Update What I am really looking for is that angular should not attach the forward slash neither remove the hash sign.

like image 351
tusharmath Avatar asked May 21 '14 13:05

tusharmath


People also ask

What is HTML5mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.

What is $location in AngularJS?

The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.

How to use location path in AngularJS?

This method is getter / setter. Return path of current URL when called without any parameter. Change path when called with parameter and return $location . Note: Path should always begin with forward slash (/), this method will add the forward slash if it is missing.

What is $window in AngularJS?

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.


1 Answers

@Tushar I'm not sure if you've figured out a solution but I came across your scenario too and no luck with googling. Eventually I figured out it's a rather simple fix, I've added : -

angular.config(function($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false,
        rewriteLinks: false
    });
})

And it just stop appending the forward slash (/) prefix to my hash anchor. Everything remains as what we're familiar with (no replacing of URL with hash or what-not).

like image 167
aheryan Avatar answered Oct 06 '22 18:10

aheryan