问题
I'm having issues with onchange event for file input. Here is my code:
Html:
<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" onchange="fileUploaded(event)"/>
Angularjs code - using 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);
};
I tried to follow this and change accordingly but am still having issues. Pass angularJS $index into onchange
I changed like this.
<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" onchange="angular.element(this).scope().fileUploaded(this)">
I got this error. Uncaught TypeError: undefined is not a function. onchange
When I am trying to use ng-change, I have this error.
<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>
This gives error when accessing this $scope.importFile.center(); "TypeError: Cannot read property 'center' of undefined" Again, works fine if I use ng-click.
Any help on this is higly appreciated.
Thanks!
回答1:
You cannot use ngChange
in this case since ngChange
always requires ngModel
and ngModel
doesn't work with input type file. Also, you cannot access a function binded to $scope
directly using onchange
. Instead use the following:
onchange="angular.element(this).scope().fileUploaded($event)"
来源:https://stackoverflow.com/questions/35816670/function-is-not-defined-referecenerror-onchange-in-angularjs