我想用AngularJS拿起一个文件:
HTML:
<div ng-controller="TopMenuCtrl"> <button class="btn" ng-click="isCollapsed = !isCollapsed">Toggle collapse</button> <input type="file" ng-model="filepick" ng-change="pickimg()" multiple /> <output id="list"></output> </div>
JavaScript的:
angular.module('plunker',['ui.bootstrap']); function TopMenuCtrl($scope) { $scope.pickimg = function() { alert('a'); }; }
Angular不支持输入[type = file]的更改,因此您必须自己滚动更改实现。
首先,在HTML中,定义Javascript for forchange,如下所示:
<input ng-model="photo" onchange="angular.element(this).scope().file_changed(this)" type="file" accept="image/*" />
$scope.file_changed = function(element) { $scope.$apply(function(scope) { var photofile = element.files[0]; var reader = new FileReader(); reader.onload = function(e) { // handle onload }; reader.readAsDataURL(photofile); }); };