AngularJS Tutorial - ng-change calls function in scope








The following code shows how to use ng-change to call function in scope.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
var myApp = angular.module('myApp', []);
<!--  ww w.j  a va  2  s  . c  o m-->
function MyCtrl($scope) {
    $scope.value = '-';
    $scope.newValue = function(value) {
       $scope.value = value;
    }
}
</script>
</head>
<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <span ng-repeat="i in [0, 1, 2]">
       <input name="asdf" type="radio" ng-model="value" value={{i}} ng-change='newValue(value)'>
    </span>
{{value}}
</body>
</html>

The code above is rendered as follows: