Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - using ng-show within ng-repeat

Tags:

I'm having an issue using the ng-show directive within an ng-repeat block.

The boolean value does not seem to be getting passed to ng-show correctly...

To show what I mean, here is a screenshot of an example I made in JSFiddle:

enter image description here

Here is some example markup:

<table ng-controller="ActressController" class="table table-bordered table-striped">     <tr ng-repeat="actress in actressList">         <td>             <span class="actress-name">{{ actress.name }}</span>             <h4 ng-show="{ actress.name == 'Scarlett' }">Was in Avengers! <span class="note">(should only appear if Scarlett)</span></h4>             <h2>{{ actress.name == 'Scarlett'}} <span class="note"><-- this statement is correct</span></h2>          </td>      </tr> </table> 

Here is an example controller:

function ActressController($scope) {     $scope.actressList = [         {             name: "Angelina"         }, {             name: "Scarlett"         }, {             name: 'Mila'         }, {             name: 'Megan'         }     ]          } 

Any ideas on what I may be doing wrong?

like image 652
PhillipKregg Avatar asked Oct 19 '12 17:10

PhillipKregg


People also ask

Can we use ng show and Ng hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design. You can use a function, another field or just some more inline-logic.

How do you pass an index in NG-repeat?

Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope. So what you need to do is reach up to the parent scope, and use that $index .

What can I use instead of NG-repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do you use two ng-repeat in a table?

You can nest two ng-repeat together in order to create a more complex table. The first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection.


1 Answers

In your ng-show you don't need { } try this:

<h4 ng-show="actress.name == 'Scarlett'">Was in Avengers! <span class="note"> 

See this fiddle for a working sample of an ng-show within an ng-repeat.

like image 149
Gloopy Avatar answered Nov 02 '22 19:11

Gloopy