我在处理文件输入的 onchange 事件时遇到问题。 这是我的代码:
HTML:
<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" onchange="fileUploaded(event)"/>
Angularjs 代码 - 使用 Typescript。
$scope.fileUploaded = (event) => {
console.log("Testing file upload");
var reader = new FileReader();
var target = event.target || event.srcElement;
reader.onload = function(e) {
$scope.$apply(function() {
$scope.readFile = reader.result;
});
};
var file = target.files[0];
reader.readAsText(file);
};
我尝试按照此进行相应更改,但仍然遇到问题。 Pass angularJS $index into onchange
我是这样改的。
<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" onchange="angular.element(this).scope().fileUploaded(this)">
我遇到了这个错误。 未捕获的类型错误:未定义不是函数。 改变
当我尝试使用 ng-change 时,我遇到了这个错误。
<div kendo-window="importFile" k-visible="false" k-modal="true" k-title="'Import File'"
k-min-width="450" k-max-width="450"
k-min-height="418" k-max-height="418">
<form kendo-validator="validator" ng-submit="validate($event)">
<li style="padding-bottom: 5px;">
<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" ng-change="fileUploaded($event)"/>
</li>
</form>
</div>
访问时会报错 $scope.importFile.center(); “类型错误:无法读取未定义的属性‘中心’” 同样,如果我使用 ng-click,效果很好。
非常感谢对此的任何帮助。
谢谢!
最佳答案
在这种情况下,您不能使用 ngChange
,因为 ngChange
总是需要 ngModel
而 ngModel
不适用于输入类型文件。此外,您不能直接使用 onchange
访问绑定(bind)到 $scope
的函数。而是使用以下内容:
onchange="angular.element(this).scope().fileUploaded($event)"
关于html - Angularjs 中未定义 ReferecenError onchange 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816670/