【问题标题】:Function is not defined ReferecenError onchange in AngularjsAngularjs中未定义函数ReferecenError onchange
【发布时间】: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


【解决方案1】:

在这种情况下您不能使用ngChange,因为ngChange 始终需要ngModel,而ngModel 不适用于输入类型文件。此外,您不能直接使用onchange 访问绑定到$scope 的函数。而是使用以下内容:

onchange="angular.element(this).scope().fileUploaded($event)"

【讨论】:

  • 感谢 Tarun,我在尝试按照您建议的方式使用时出现此错误。 ReferenceError: $event 未定义 onchange
  • 将 $event 替换为 'this'
  • 嗨,Tarun,感谢您的建议,但我之前尝试过。它在我上面的代码中。我得到了这个错误。 Uncaught TypeError: undefined is not a function。改变
  • 有什么办法可以解决问题,因为在我正在使用的代码中,他们设置了这样的内容:var _this = this;他们不做 $scope.functionName,而是做 _this.functioName。所以当我使用这个方法时,它会返回一个错误。有什么线索吗?
猜你喜欢
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
相关资源
最近更新 更多