Store file name to scope for file input

Description

The following code shows how to store file name to scope for file input.

Example


<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
</head><!--   w  w  w .j  a va 2 s . c  o m-->
<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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities