Function is not defined ReferecenError onchange in Angularjs

こ雲淡風輕ζ 提交于 2020-01-04 05:46:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!