Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "href" and "ng-href" in AngularJS

Tags:

I've used both href and ng-href and I couldn't see the difference between them.

Why does Angular have the ng-href attribute, and when should I use it?

like image 973
Andre Figueiredo Avatar asked May 26 '16 17:05

Andre Figueiredo


People also ask

What is the use of href in angular?

href is a tag that is used to route the user to different web pages. href allows users to visit an external website from the current website. It is mostly used in anchor tags.

What is the difference between ng model and data NG model?

For AngularJS there is no difference between ng-app and data-ng-app or ng-controller and data-ng-controller or ng-model and data-ng-model because while compiling HTML page, AngularJS strips data- or x- prefix. Find the URL for reference.

What is HTML href?

The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!


1 Answers

From the documentation:

Using Angular markup like {{hash}} in an href attribute will make the link go to the wrong URL if the user clicks it before Angular has a chance to replace the {{hash}} markup with its value. Until Angular replaces the markup the link will be broken and will most likely return a 404 error. The ngHref directive solves this problem.

Effectively, the only place you're using it is for links in which you need to rely on a value provided to the DOM by Angular. If you do not require Angular for a part of that link, or you don't plan on using Angular to generate that link, then you do not need to use ngHref.

like image 144
Makoto Avatar answered Oct 04 '22 05:10

Makoto