【发布时间】:2016-06-19 09:43:45
【问题描述】:
我遇到文件输入的 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)">
我收到了这个错误。 Uncaught TypeError: undefined is not a function。 改变
当我尝试使用 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(); “TypeError:无法读取未定义的属性‘中心’” 同样,如果我使用 ng-click,效果很好。
非常感谢您对此的任何帮助。
谢谢!
【问题讨论】:
-
你必须使用
ng-change()angular 函数而不是onChange()普通的javascript函数。 -
将输入的属性更改为
ng-change="fileUploaded($event)"。 -
感谢吉姆和高拉夫!但是,在尝试使用 ng-change 时,我仍然收到另一个错误。我已经更新了我的帖子。
-
$scope.importReport是什么东西?你的代码中是否有它? -
嗨,Ammar,我的错误是 $scope.importFile.center()。我改变了它。它是一个剑道窗口。
标签: html angularjs typescript onchange