Javascript angularjs-如何在不使用getElementById的情况下获取输入标记中的文件属性
我正在用AngularJS上传文件。 如何像普通JS一样获取输入文件 这就是我需要精确模拟的内容Javascript angularjs-如何在不使用getElementById的情况下获取输入标记中的文件属性,javascript,html,angularjs,file-upload,ng-file-upload,Javascript,Html,Angularjs,File Upload,Ng File Upload,我正在用AngularJS上传文件。 如何像普通JS一样获取输入文件 这就是我需要精确模拟的内容 HTML: 这就是我现在拥有的 HTML: 那么,如何使用“getElementById”获取输入withput中的“文件”? 注意-不能更改任何内容,只能更改“???????” 任何帮助都将不胜感激。 tnx您可以使用自定义指令 var app = angular.module('App', []); app.controller('Main', function($scope, $timeou
HTML: 这就是我现在拥有的
HTML: 那么,如何使用“getElementById”获取输入withput中的“文件”?
注意-不能更改任何内容,只能更改“???????” 任何帮助都将不胜感激。
tnx您可以使用自定义指令
var app = angular.module('App', []);
app.controller('Main', function($scope, $timeout, $rootScope){
$scope.onSubmit = function(){
console.log($scope.file);
}
});
app.directive('fileRead', [function () {
return {
scope: {
fileRead: '='
},
link: function (scope, elem, attrs) {
elem.bind('change', function (event) {
scope.$apply(function () {
scope.fileRead = event.target.files[0];
});
});
}
}
}]);
用法
提交
很遗憾,我不能使用指令。糟糕,我知道。无论如何谢谢你,伙计。这是一个很好的解决方案,如果你不想使用指令,你可以在输入字段中添加一些id,然后在提交处理程序中以这种方式获取文件var fileElement=document.getElementById('file')代码>var files=fileElement.files代码>这是一个限制,不是我的愿望。而你这个有ID的人,现在是不可能的。
var filesUpload = document.getElementById("fileImg").files
<input type="file" name="file" ng-model="image" accept="image/*">
<input type="button" ng-click="submit()" value="press me">
angular
.module('app')
.controller("productsCtrl", function($scope) {
$scope.image = {};
$scope.submit = function() {
var filesUpload = ????????????????
}
}
var app = angular.module('App', []);
app.controller('Main', function($scope, $timeout, $rootScope){
$scope.onSubmit = function(){
console.log($scope.file);
}
});
app.directive('fileRead', [function () {
return {
scope: {
fileRead: '='
},
link: function (scope, elem, attrs) {
elem.bind('change', function (event) {
scope.$apply(function () {
scope.fileRead = event.target.files[0];
});
});
}
}
}]);
<div ng-app="App">
<div ng-controller="Main">
<form ng-submit="onSubmit()">
<input type="file" name="someName" file-read="file">
<button type="submit">Submit</button>
</form>
</div>
</div>