Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 763
Adrian Gunawan Avatar asked Jan 30 '13 03:01

Adrian Gunawan


2 Answers

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

like image 114
Mark Rajcok Avatar answered Sep 21 '22 17:09

Mark Rajcok


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;

    }
like image 33
Luis Mesejo Avatar answered Sep 17 '22 17:09

Luis Mesejo