conditional ng-repeat

3,737 views
Skip to first unread message

wingy

unread,
Aug 3, 2012, 7:55:47 PM8/3/12
to ang...@googlegroups.com
I need to iterate through an array of items to create a HTML element for each one but only if the current item does not match a specific string.

Here is my code:

    <a href="#/{{item}}" ng-repeat="item in items">{{item}}</a>

How do I change that one to iterate all except when item === "foo"?

Wingy

Dan Doyon

unread,
Aug 3, 2012, 7:58:56 PM8/3/12
to ang...@googlegroups.com
Use a filter, take a look at the list.html in the javascript projects example on honepage

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

wingy

unread,
Aug 3, 2012, 8:07:08 PM8/3/12
to ang...@googlegroups.com
I added this one to the element and it worked great:

ng-hide="itemType == routeParams.itemTypeId"

I'll have a look at the filter example.

Wingy

wingy

unread,
Aug 3, 2012, 8:08:09 PM8/3/12
to ang...@googlegroups.com
The bad thing about my solution is that it still creates the element but hiding it I think.

Is there a way to write this with ng-switch? Can't get how it should be done.

Wingy

Dan Doyon

unread,
Aug 3, 2012, 8:18:32 PM8/3/12
to ang...@googlegroups.com
I'll get u an ex later on (barring someone else doesn't )

Sent from my iPhone
--

Pawel Kozlowski

unread,
Aug 4, 2012, 3:25:57 AM8/4/12
to ang...@googlegroups.com
Hi Wingy!

As suggested by Dan filters are perfect match for the filtering
use-cases as yours. Please find some jsFiddles showing what is
possible:

1) Filtering out items that got 'foo' inside:
http://jsfiddle.net/pkozlowski_opensource/vA8zq/1/
2) Filtering out items strictly equal to 'foo':
http://jsfiddle.net/pkozlowski_opensource/vA8zq/2/

The version with ng-switch would work but it would be verbose since
you would be writing filtering logic in a template.

Hope this helps,
Pawel

Adriaan Bouman

unread,
Aug 4, 2012, 9:08:21 AM8/4/12
to ang...@googlegroups.com
A fiddle with an example: http://jsfiddle.net/adriaanb/cDWVr/6/

http://docs.angularjs.org/api/ng.filter:filter 

Op zaterdag 4 augustus 2012 01:55:47 UTC+2 schreef wingy het volgende:

Dan Doyon

unread,
Aug 4, 2012, 11:54:40 AM8/4/12
to ang...@googlegroups.com
Here is another example (I did not create) that shows how to work with object hashes.

http://plunker.no.de/edit/v2AYQg

wingy

unread,
Aug 4, 2012, 10:07:40 PM8/4/12
to ang...@googlegroups.com
Thanks for all suggestions. Using a filter was a great solution!

Wingy
Reply all
Reply to author
Forward
0 new messages