`(带指令 DEMO)-6ren"> `(带指令 DEMO)-我尝试在类型文件的输入标签上使用 ng-model: 但是在选择文件之后,在 Controller 中,$scope.vm.uploadme 仍然是未定义的。 如何在我的 Controller 中获-6ren">
gpt4 book ai didi

angularjs - `<input type="文件的 ng-model"/>`(带指令 DEMO)

转载 作者:行者123 更新时间:2023-11-28 02:31:11 25 4
gpt4 key购买 nike

我尝试在类型文件的输入标签上使用 ng-model:

<input type="file" ng-model="vm.uploadme" />

但是在选择文件之后,在 Controller 中,$scope.vm.uploadme 仍然是未定义的。

如何在我的 Controller 中获取选定的文件?

最佳答案

我用指令创建了一个解决方法:

.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
scope.$apply(function () {
scope.fileread = loadEvent.target.result;
});
}
reader.readAsDataURL(changeEvent.target.files[0]);
});
}
}
}]);

输入标签变为:

<input type="file" fileread="vm.uploadme" />

或者如果只需要文件定义:

.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
scope.$apply(function () {
scope.fileread = changeEvent.target.files[0];
// or all selected files:
// scope.fileread = changeEvent.target.files;
});
});
}
}
}]);

关于angularjs - `&lt;input type="文件的 ng-model"/>`(带指令 DEMO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com