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

17,214

Solution 1

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

Solution 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;

    }
Share:
17,214
Adrian Gunawan
Author by

Adrian Gunawan

Updated on June 13, 2022

Comments

  • Adrian Gunawan
    Adrian Gunawan about 2 years

    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

  • Adrian Gunawan
    Adrian Gunawan over 11 years
    ahh i thought that didn't work. Maybe because its rendered as remove($index) and I assume it won't work. Thanks heaps!!
  • K-Dawg
    K-Dawg over 9 years
    Can you give an example of this?
  • Mark Rajcok
    Mark Rajcok over 9 years
    @PrimeByDesign, bring up Adrian's fiddle, and make the change I suggest: <a href="#" ng-click="remove($index)">X (works now!)</a>.