AngularJS Tutorial - ng-change








ng-change directive evaluates the given expression when the input changes.

Example

The following code shows how to ng-change.


<!--   w ww  . ja  v  a 2  s.co m-->

<!doctype html>
<html ng-app="myApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script>
</head>
<body>
  <div ng-controller="MyController">
    <p>type any number into the text field</p>
    <input type="text" ng-model="equation.x" ng-change="change()" />
    <code>{{ equation.output }}</code>
  </div>
  <script>
    angular.module('myApp', [])
    .controller('MyController', function($scope) {
      $scope.equation = {};
      $scope.change = function() {
           $scope.equation.output = Number($scope.equation.x) +" is the new value";
      };
    });
  </script>
</body>
</html>

The code above is rendered as follows: