Skip to content
Related Articles

Related Articles

AngularJS | ng-bind Directive
  • Last Updated : 30 Mar, 2021

The ng-bind Directive in AngularJS is used to bind/replace the text content of any particular HTML element with the value that is entered in the given expression. The value of specified HTML content updates whenever the value of the expression changes in ng-bind directive.
Syntax: 
 

<element ng-bind="expression"> Contents... </element>

Where expression is used to specify the expression to be evaluated or the variable.
Example 1: This example uses ng-bind Directive to bind the product of two number to the <span> element. 
 

html




<!DOCTYPE html>
<html>
     
<head>
    <title>ng-bind Directive</title>
 
    <script src=
    </script>
</head>
 
<body ng-app="gfg" style="text-align:center">
     
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-bind Directive</h2>       
     
    <div ng-controller="app">
        num1: <input type="number" ng-model="num1"
                ng-change="product()" />
        <br><br>
         
        num2: <input type="number" ng-model="num2"
                ng-change="product()" />
        <br><br>
         
        <b>Product:</b> <span ng-bind="result"></span>
    </div>
     
    <script>
        var app = angular.module("gfg", []);
        app.controller('app', ['$scope', function ($app) {
            $app.num1 = 1;
            $app.num2 = 1;
            $app.product = function () {
                $app.result = ($app.num1 * $app.num2);
            }
        }]);
    </script>
</body>
 
</html>

Output: 
 

ng-bind

Example 2: This example uses ng-bind Directive to bind the innerHTML of the <span> element to the variable text. 
 



html




<!DOCTYPE html>
<html>
     
<head>
    <title>ng-bind Directive</title>
 
    <script src=
    </script>
</head>
 
<body  style = "text-align:center">
     
    <h1 style = "color:green">GeeksforGeeks
    <h2 style = "">ng-bind directive</h2>
     
    <div ng-app="" ng-init="txt='GeeksforGeeks';col='green'">
        <div>
            <span ng-bind="txt"></span> is the
            computer science portal for geeks.
        </div>
    </div>
</body>
 
</html>

Output: 
 

ng-bind

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :