功能在Angularjs定义ReferecenError的onchange [英] Function is not defined ReferecenError onchange in Angularjs

查看:335
本文介绍了功能在Angularjs定义ReferecenError的onchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与onchange事件的文件输入的问题。
这里是我的code:

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 - 使用打字稿。

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);
        };

我试图按照此并相应地改变,但我仍然有问题。
通angularJS $索引的onchange

我改变了这个样子。

<input type="file" kendo-upload k-options="uploadOptions" id="fileInput" onchange="angular.element(this).scope().fileUploaded(this)">

我得到这个错误。
未捕获类型错误:未定义是不是一个函数。
的onchange

I got this error. Uncaught TypeError: undefined is not a function. onchange

当我试图用NG-变化,我有这样的错误。

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>

此访问时,这给了错误
$ scope.importFile.center();
类型错误:无法读取的未定义的属性'中心'。
同样,如果我用NG-点击工作正常。

This gives error when accessing this $scope.importFile.center(); "TypeError: Cannot read property 'center' of undefined" Again, works fine if I use ng-click.

任何帮助是higly AP preciated。

Any help on this is higly appreciated.

谢谢!

推荐答案

您不能在这种情况下,使用 ngChange ,因为 ngChange 总是需要 ngModel ngModel 不随输入类型的文件工作。此外,您不能访问直接使用的onchange 绑定到 $范围的功能。相反,使用以下命令:

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)"

这篇关于功能在Angularjs定义ReferecenError的onchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆