Aditya
Aditya

Reputation: 431

Why should I put '#' before the path when routing with AngularJS?

When routing in Angular views we add the following. I don't understand the need to add #; if I remove it, I get a 404 Error.

a href="#/AddNewOrder"

Upvotes: 0

Views: 206

Answers (3)

hon2a
hon2a

Reputation: 7214

Putting # in URL indicates start of the hash part, which is used to address elements inside a single page. In modern single-page web applications, this can be used to address application states.

If you don't put the # there, you're changing the path, which means you're creating a new URL and prompting the browser to load the content at that new URL instead of the current page.


As other posters have suggested, you don't have to use hashes when using html5mode. I left it out, because it brings a few challenges of its own, which I feel to be outside the scope of the question.

Upvotes: 1

arghya
arghya

Reputation: 123

# or fragment identifier is a way to indicate a specific portion of a single document. Without the #, the url corresponds to a different page.

For example www.yoursite.com/page links to the /page location of your website, while www.yoursite.com/#/location points to the same index page of your website but at a specfic point in the web page #location, or in your case, a different template view.

Angular routing can not load different templates for different server urls. It is specifically designed for single-page applications and any loading of partial views or templates has to happen on the same web-page or location. Hence only the fragment part of the url changes when using angularjs routing.

More information about fragments can be found here: http://en.wikipedia.org/wiki/Fragment_identifier

Upvotes: 0

user1177476
user1177476

Reputation:

enter link description hereYou don't have to. You can configure your URLs to look like normal URLs, but in reality they will still work the same way.

Check https://docs.angularjs.org/guide/$location And refer to html5mode

It will only work in modern browsers though. Old browsers will still show the hash. But the cool thing is that you can write your URLs the old/normal way.

Upvotes: 1

Related Questions