What is 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.

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.

ng-href is a parameter that overrides the original href parameter. This allows Angular to check if the link is broken or not before the code is compiled. ng-href also allows the links to be used dynamically. This is explained in the code below.

Code

The following code uses href as a static link and ng-href as a dynamic link.

<!-- Static linking -->
<a href="http://educative.io" />Visit the Educative's Static Link</a>
<!-- Dynamic linking in Angular using ng-href -->
<div ng-init="newLink = 'http://educative.io'">
<p>Visit <a ng-href="{newLink}}">Educative's dynamic link</a></p>
</div>

Free Resources