ng href directives in angularjs

ng-href directives in angularjs

The ng-href is a angular directive just like href tag both are working same. The ng-href directive is standard way to use angularjs and bind it dynamically URLs to user data. 

If not, your href are broken when they contain interpolation directives.

for example. The hreftag will look like in angular

<div>
    <a href="http://facebook.com/{{ username }}" target="_self"  title="http://facebook.com/{{username}} >Facebook Page</a>
</div>

for example. in angular ng-href  directive will look like.

<div>
    <a ng-href="http://facebook.com/{{username}}" target="_self" title="http://facebook.com/{{username}}">My Facebook Page..</a>
</div>

The detail example of ng-href  directive as given below.

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8">

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
    <script type="text/javascript">
        var ng = angular.module("app", []);

        ng.controller("controller", function ($scope) {
            $scope.username = "anilsinghkushinagar";
        });
    </script>
</head>

<body ng-app="app">
    <div ng-controller="controller">
        <div>Click on below links.</div>
        <div>
            <a ng-href="http://facebook.com/{{username}}" title="http://facebook.com/{{username}}" target="_self">My Facebook Page..</a>
        </div>
        <div>
            <a href="http://facebook.com/{{ username }}">Facebook Page</a>
        </div>
    </div>
</body>

</html>

The output look like below.




ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.