Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set ngClick only for $first element in ngRepeat?

Tags:

angularjs

How can ng-click be assigned to the $first element in ng-repeat? In the below code doSomething() should be called only when the first input is clicked. The remaining inputs shouldn't have ng-click handlers assigned. I've found examples of how to use $first with ng-class or ng-show but not with ng-click.

<div ng-repeat="item in items">
  <input type="checkbox" ng-click="doSomething()"/>{{item.name}}
</div>
like image 780
jEremyB Avatar asked Dec 30 '13 16:12

jEremyB


2 Answers

Try this:

<input type="checkbox" ng-click="$first && doSomething()"/>{{item.name}}

working examlpe: http://jsfiddle.net/wLrYc/

like image 84
CD.. Avatar answered Oct 21 '22 09:10

CD..


You could also use ngSwitch which avoids creating all the handlers which you mention in your comment to the accepted answer:

<div ng-repeat="item in items" ng-switch on='$first'>
    <input type="checkbox" ng-switch-when='true' ng-click="doSomething()"/>
    <input type="checkbox" ng-switch-when='false'/>
    {{item.name}}
</div>
like image 33
Gruff Bunny Avatar answered Oct 21 '22 07:10

Gruff Bunny