AngularJS ng-click not invoked with {{$index}} used

自古美人都是妖i 提交于 2020-01-01 04:31:06

问题


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


回答1:


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




回答2:


solved!

<div ng-repeat="idiomax in textos.idiomas ">
    <div class="idioma"  ng-click="cambiaridioma($index)" ng-class="idioma != $index || 'idioma-activo'" > 
    {{idiomax.idioma}}
    </div>
</div>

$scope.cambiaridioma = function (indice) {
        $scope.idioma = indice;

    }



回答3:


I had to use $index in a string expression and this is how it worked:

HTML

<button ng-click="showUserStories($event, '#modal-id-prefix-' + $index)">Show Modal</button>

Script

$scope.showUserStories = function(event, modalId) {
        $(modalId).modal("show");
        event.stopPropagation();
    }


来源:https://stackoverflow.com/questions/14596235/angularjs-ng-click-not-invoked-with-index-used

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