Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-show for a specific element within ng-repeat

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.

like image 669
Christian Benke Avatar asked Mar 20 '13 15:03

Christian Benke


People also ask

How do you condition in NG-repeat?

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.

How do I get the index of an element in NG-repeat?

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.

How do you put filters on NG-repeat?

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.

Where is the last element in NG-repeat?

$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.


1 Answers

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/

like image 152
odiseo Avatar answered Sep 20 '22 00:09

odiseo