Open In App

AngularJS ng-bind Directive

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 the ng-bind directive.

Syntax:

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

Parameter value:

  • expression: It is used to specify the expression to be evaluated or the variable.

Example 1: This example implements the ng-bind Directive to bind the product of two numbers 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>
    <h3>ng-bind Directive</h3>
    <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:

 

Example 2: This example implements the 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
    </h1>
    <h3>ng-bind directive</h3>
    <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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads