嗨,我是 Angular js 新手,我想尝试在 html 中获取我的 input type = "file"
的数据。但是当我控制台它时,它只是说未定义
html
<input type="file" ng-model="data.img" ng-click="upload()"/>
js
$scope.upload = function() {
console.log($scope.data.img)
}
最佳答案
ng-model 不支持文件类型输入。需要使用这样的指令手动将值设置为 ngModel
.directive("filesInput", function() {
return {
require: "ngModel",
link: function postLink(scope,elem,attrs,ngModel) {
elem.on("change", function(e) {
var files = elem[0].files;
ngModel.$setViewValue(files);
})
}
}
})
像这样调用指令
<input type="file" files-input ng-model="data.img" ng-click="upload()"/>
现在检查控制台
关于javascript - Angularjs 输入类型 = 文件不控制台数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626770/