As far as I know, AngularJS applications are supposed to be single-page. That also should be true for web forms.
I can easily process a text input field:
var app = angular.module('snippet', []);
app.controller('MainCtrl', function($scope) {
$scope.submit = () => {
$scope.result = $scope.inputText;
}
});
h1 {
font-family:Arial;
font-size:14pt;
}
<!DOCTYPE html>
<html ng-app="snippet">
<head>
<meta charset="utf-8" />
<title>AngularJS snippet</title>
<link rel="stylesheet" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<h1>Enter text:</h1>
<input ng-model="inputText">
<input type="button" ng-click="submit()" value="submit">
</div>
<div>
<h1>Text you have entered:</h1>
<span>{{result}}</span>
</div>
</body>
</html>
However, when I change input type="text"
to input type="file"
and try to specify a text file, the $scope.result
property remains undefined:
var app = angular.module('snippet', []);
app.controller('MainCtrl', function($scope) {
$scope.submit = () => {
$scope.result = $scope.inputFile;
}
});
h1 {
font-family:Arial;
font-size:14pt;
}
<!DOCTYPE html>
<html ng-app="snippet">
<head>
<meta charset="utf-8" />
<title>AngularJS snippet</title>
<link rel="stylesheet" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<h1>Enter text:</h1>
<input type="file" accept=".txt" ng-model="inputFile">
<input type="button" ng-click="submit()" value="submit">
</div>
<div>
<h1>Text you have entered:</h1>
<span>{{result}}</span>
</div>
</body>
</html>
How should I modify the snippet above to make it works with a text file?