Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat + ng-click not working as expected

Tags:

angularjs

Seems that this is a very common issue with angular. The substitutions happen, but the ng-click fails to click (or so it seems to not be called). Can someone tell me why this fails?

<ul>
     <li ng-repeat="color in colors"> 
         <a ng-click="chooseColor({{$index}})">{{color.name}}</a>
     </li>
</ul>

here is the fiddle.

It seems to be the {{$index}}. If i replace that with a static number, it works.

Note: found this question - but it was not helpful.

like image 926
nycynik Avatar asked Dec 09 '22 16:12

nycynik


1 Answers

The {{}} bits are used templating (interpolation). This should work for you:

ng-click="chooseColor($index)"

The ng-click directive is looking for real JavaScript code an Angular Expression, not text that it needs to interpolate.

like image 144
Langdon Avatar answered Jan 15 '23 22:01

Langdon