AngularJS – ng-change Directive

AngularJSWeb DevelopmentFront End Technology

The ng-change directive in AngularJS evaluates a given expression whenever the user changes the input. On every change, the ngChange directive is fired and the expression is evaluated immediately. The JavaScript onChange event only triggers at the end of a change (when the user leaves the form element or presses the <Enter> key).

Note − This directive also requires ngModel to be present.

Syntax

<element ng-change="expression">..Content..</element>

Example − ngChange Directive

Create a file "ngChange.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>ngChange Directive</title>

      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
      </script>
   </head>

   <body style="text-align: center;">
   <h1 style="color:green">
      Welcome to Tutorials Point
   </h1>
      <h2>
         AngularJS | ngChange Directive
   </h2>

      <div ng-app="demo" ng-controller="app">
         Check to show/hide details
         <input type="checkbox" ng-change="show()" ng-model="check">
         <br><br>

         <div style="padding:50px; margin-left: auto; margin-right: auto; border:1px solid black; width:30%;color:green" ng-show='result'>
            Start Learning the new Angular JS features now...
         </div>
      </div>

      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module('demo', []);
         app.controller('app', function ($scope) {
            $scope.show = function () {
               if ($scope.check == true)
                   $scope.result = true;
               else
                   $scope.result = false;
            }
         });
      </script>

   </body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Example 2

Create a file "ngChange.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>ngChange Directive</title>

      <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
      </script>
   </head>

   <body style="text-align: center;">
   <h1 style="color:green">
      Welcome to Tutorials Point
   </h1>
      <h2>
         AngularJS | ngChange Directive
   </h2>

      <div ng-app="demo" ng-controller="prop">
         <div>
            Are you a developer:
            <input type="checkbox" ng-model="isTrue" ng-change="cnt=cnt+1" ng-init="cnt=0"/>
         </div>
         Count: {{cnt}}
         <pre>{{isTrue}}</pre>
      </div>

      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("demo", []);
         app.controller('prop', ['$scope', function ($app) {
            $app.isTrue = false;
         }]);
      </script>

   </body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

raja
Published on 08-Oct-2021 09:29:23
Advertisements