Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have traditional anchor links in Angular.js applications

Angular.js routes create URLs such as these:

http://cooldomain.com:3000/#/search

http://cooldomain.com:3000/#/docs

In my docs url, I would like to have one long page with <a name="sdsds"> sections and a traditional table of content with anchor links so that the user can hop up and down the page

Conceptually the table of contents would produce lots of invalid URLs such as http://cooldomain.com:3000/#/docs#coolAPIFunction which of course wouldn't work because of the double hash

So- is it possible to use anchor links in Angular.js applications that have routes?

like image 281
Fergie Avatar asked Nov 01 '13 14:11

Fergie


People also ask

Can we use anchor tag in angular?

Anchor tags are the most common HTML tags that take href as a parameter and route users to another website. In Angular, developers should use ng-href instead of href in the anchor tags.

Which function is used to create an anchor link?

<a>: The Anchor element. The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address. Content within each <a> should indicate the link's destination.

What is the method of using hyperlink and anchors?

The <a> element, or anchor element, it used to create a hyperlink to another webpage or another location within the same webpage. The hyperlink created by an anchor element is applied to the text, image, or other HTML content nested between the opening and closing <a> tags.


1 Answers

You could enable html5 pushstate and get rid of the # in your routes. You can do so by adding this to your .config

$locationProvider.html5Mode(true);

However, be aware that now there will not be a distinction between Angular routes vs. server requests. You'll have to config your server to deliver the appropriate static html file (e.g. index.html) for that url.

like image 185
Shu Avatar answered Oct 12 '22 23:10

Shu