通过angularJS $索引的onchange [英] Pass angularJS $index into onchange

查看:1465
本文介绍了通过angularJS $索引的onchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angularJS应用一个NG重复内的输入文件。我需要将$索引变​​量传递给onchange属性。我使用的onchange不NG-的变化,因为我需要上传的对象(看到这个帖子

在以下code时得到未捕获的Ref​​erenceError:$指数没有定义

玉code样品:<​​/ P>

  div.input组(NG-重复='在文件名中的文件名通过跟踪​​指数$')
    输入(类型='文件',的onchange =angular.element(本).scope()。file_changed(this.files,** $指数**))


解决方案

的onchange 属性,范围仅通过访问 angular.element (本).scope()。这是你使用的方法来调用 file_changed()功能,你应该以有机会获得 $指数属性:

 &LT;输入类型=文件的onchange =angular.element(本).scope()file_changed(this.files,angular.element(本).scope() 。$指数)/&GT;

请注意,这是成为pretty长!一个解决办法是简单的DOM元素传递给函数,并从中获取所有信息:

 &LT;输入类型=文件的onchange =。angular.element(本).scope()file_changed(本)/&GT;

  $ scope.file_changed =功能(元素){
    VAR指数= angular.element(元素).scope()$指数。
    var中的文件= element.files;    // ...
};

I have an input file within a ng-repeat in my angularJS app. I need to pass the $index variable to the onchange attribute. I'm using onchange and not ng-change because I need the uploaded object (see this post)

In the following code I get 'Uncaught ReferenceError: $index is not defined'

Jade code sample:

div.input-group(ng-repeat='filename in filenames track by $index') 
    input(type='file', onchange="angular.element(this).scope().file_changed(this.files, **$index**)")

解决方案

In the onchange attribute, the scope is only accessible via angular.element(this).scope(). That's the method you use to call the file_changed() function, and you should use the same in order to have access to the $index attribute:

 <input type="file" onchange="angular.element(this).scope().file_changed(this.files, angular.element(this).scope().$index)" />

Notice that this is becoming pretty long! A solution is to simply pass the DOM element to the function, and obtain all the informations from it:

 <input type="file" onchange="angular.element(this).scope().file_changed(this)" />

$scope.file_changed = function (element) {
    var index = angular.element(element).scope().$index;
    var files = element.files;

    // …
};

这篇关于通过angularJS $索引的onchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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