I can't seem to get my head around this little problem:
<ul ng-repeat="parent in parents">
<li>Mother: <i>{{parent.mother}}</i></li>
<li>Father: <i>{{parent.father}}</i></li>
<a href="#" ng-click="showKids()">Show Kids</a>
<ul ng-repeat="kid in parent.kids" ng-show="active">
<li>{{kid.name}}</li>
</ul>
</ul>
http://jsfiddle.net/twSFK/5/
When i click "Show Kids", i only want to show the kids of the parents where i clicked, but not the other. So i'd need some kind of index for the model i use in ng-show to target only a specific element.
Since $scope.parents comes from the backend-server and is loaded with ng-init, i don't know how i can access it to add an "active"-element before the controller writes the list.
You can add condition using ng-if also and this is effective too. You can apply any condition to your list using ng-if attribute. In below example, I have put condition where Age > 20 and IsActive is true. ng-repeat will fetch all records which full fill this scenario.
Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.
The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.
$first and $last It's common when using ng-repeat to add specific behavior to the first or last element of the loop, e.g. special styling around the edges. Instead, ng-repeat already supplies you with two ready boolean properties. $first is true for the first element, and $last is true for the last element.
You can set the iterator index of ng-repeat="parent in parents"
and use it to play with two functions: showKids(index)
will mark what parent listing should go as active whenever you click on a kids listing, and isShowing(index)
in order to know what kids sublisting you should display with ng-show
I rewrote your code according to my ideas here http://jsfiddle.net/odiseo/twSFK/7/
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