AngularJS Tutorial - Call function in controller with onchange event








The following code shows how to call function in controller with onchange event.

Example


<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
</head><!--from  ww w . ja  v  a2  s  .c  om-->
<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <input type="file" 
           onchange="angular.element(this).scope().setFile(this)">
    {{theFile.name}}
</div>
<script type='text/javascript'>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
    $scope.setFile = function(element) {
        $scope.$apply(function($scope) {
            $scope.theFile = element.files[0];
        });
    };
});
</script>
</body>
</html>

The code above is rendered as follows: