AngularJS Tutorial - Use ng-change to change scope value








The following code shows how to use ng-change to change scope value.

Example


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function Ctrl($scope, $window) {<!--from   w w  w .  j av a  2 s .  co  m-->
    $scope.window = $window;
}
</script>
</head>
<body>
  <div ng-app ng-controller="Ctrl">
  <label>Name:</label>
  <input type="text"  
         ng-model="yourNameInput" 
         placeholder="Enter a name here" 
         ng-change="window.yourName = yourNameInput">
  <input type="button" onClick="console.log(yourName);" value="Log">
  <hr>
  <h1 ng-bind-template="Hello, {{yourNameInput}}"></h1>
</div>
</body>
</html>

The code above is rendered as follows: