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>
Try this:
<input type="checkbox" ng-click="$first && doSomething()"/>{{item.name}}
working examlpe: http://jsfiddle.net/wLrYc/
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With