The ng-href directive is used when we have an angular expression inside the href value. If href attribute is used then the problem is that if in case the link is clicked before AngularJS has replaced the expression with its value then it may go to the wrong URL and the link will be broken and will most likely return a 404 error while ng-href directive checks that the link is not broken even if the link is clicked before the code evaluation by AngularJS.
Syntax:
Content ...
Parameter value:
- addr: It refers to the string containing the angular expression.
Example 1: This example describes the use of the ng-href Directive in AngularJS, where the ng-init directive is used to initialize AngularJS Application data, & contains the URL.
<html>
<head>
<title>
AngularJS ng-href Directive
title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
script>
<style>
body {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
style>
head>
<body ng-app="" style="text-align:center">
<div ng-init="url = 'https://www.geeksforgeeks.org/'">
<h1 style="color:green">GeeksforGeeksh1>
<h2>ng-href Directiveh2>
<p>Go to
<a ng-href="{{url}}">GeeksforGeeksa>
p>
div>
body>
html>
Output:

Example 2: This is another example that describes the ng-href Directive in AngularJS.
<html ng-app="">
<head>
<title>
AngularJS ng-href Directive
title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
script>
<style>
body {
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
h1 {
color: green;
}
style>
head>
<body ng-controller="gfg">
<h1>GeeksforGeeksh1>
<h3>ng-href Directiveh3>
<a ng-href="{{ webpage }}">
GeeksforGeeks
a>
<script>
function gfg($scope) {
$scope.webpage =
"https://www.geeksforgeeks.org/";
}
script>
body>
html>
Output:
