未使用 {{$index}} 调用 AngularJS ng-click [英] AngularJS ng-click not invoked with {{$index}} used

查看:26
本文介绍了未使用 {{$index}} 调用 AngularJS ng-click的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当在 ng-click 中使用 {{$index}} 时,AngularJS 不会触发该事件.

我的 html:

<ul><li ng-repeat="foo in foos"><label>{{foo.name}}</label><a href="#" ng-click="remove({{$index}})">X(无效)</a><a href="#" ng-click="remove(0)">删除第一个元素(作品)</a>

jsfiddle: http://jsfiddle.net/Lcasg/3/

有人知道如何解决这个问题吗?谢谢

解决方案

ng-click 属性的值被评估为一个角度表达式,所以只需使用 remove($index).>

For some reason AngularJS does not fire off the event when {{$index}} is used in ng-click.

My html:

<div ng-controller="Ctrl">
  <ul>
    <li ng-repeat="foo in foos">
     <label>{{foo.name}}</label>
     <a href="#" ng-click="remove({{$index}})">X (doesnt work)</a>
     <a href="#" ng-click="remove(0)">Remove first element (works)</a>
    </li>
  </ul>
 </div>

jsfiddle: http://jsfiddle.net/Lcasg/3/

Anyone knows how to fix this? Thanks

解决方案

The value of the ng-click attribute is evaluated as an angular expression, so simply use remove($index).

这篇关于未使用 {{$index}} 调用 AngularJS ng-click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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