38

I have a simple ng-repeat that throws out data, one of fields it displays is NumberOfStamps:

<tr ng-repeat-start="list in Data.Items ">
   <td><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
   <td>(Date of Birth {[{list.Dob}]})</td>
   <td>{[{list.NumberOfStamps}]}  stamps</td>
</tr>

Example output:

Mr Adam Happy  Date of Birth 01/6/1984     16 stamps
Mr Adam Sad    Date of Birth 24/11/1975    0 stamps
Mr Adam Green  Date of Birth 02/1/1963     1 stamps
Mr Adam Red    Date of Birth 21/1/1951     12 stamps
Mr Adam Blue   Date of Birth 28/10/1998    0 stamps
Mr Adam Brown  Date of Birth 25/9/2010     0 stamps
Mr Adam Black  Date of Birth 24/8/1954     21 stamps
Mr Adam Violet Date of Birth 17/5/1942     54 stamps

How can i modify this ng-repeat to only show records where the NumberOfStams is > 0? I've tried:

<tr ng-repeat-start="list in Data.Items | filter:{NumberOfStamps > 0}">
   <td><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
   <td>(Date of Birth {[{list.Dob}]})</td>
   <td>{[{list.NumberOfStamps}]}  stamps</td>
</tr>

Expected output:

Mr Adam Happy  Date of Birth 01/6/1984     16 stamps
Mr Adam Green  Date of Birth 02/1/1963     1 stamps
Mr Adam Red    Date of Birth 21/1/1951     12 stamps
Mr Adam Black  Date of Birth 24/8/1954     21 stamps
Mr Adam Violet Date of Birth 17/5/1942     54 stamps
2
  • can you put it in a plnkr or fiddle please
    – guru
    Jun 6, 2014 at 11:41
  • @guru - is there not enough information there??
    – Oam Psy
    Jun 6, 2014 at 11:42

5 Answers 5

81

Create a predicate function on the relevant scope:

$scope.greaterThan = function(prop, val){
    return function(item){
      return item[prop] > val;
    }
}

As a first argument, it takes a property name on the object. The second argument is an integer value.

Use it in your view like this:

<tr ng-repeat-start="list in Data.Items | filter: greaterThan('NumberOfStamps', 0)">

Demo

5
  • Thanks for the post and link, whats the benefit of using/creating a predicate over other people solutions?
    – Oam Psy
    Jun 6, 2014 at 12:16
  • 1
    The solution is best expressed as a filter, close to what you originally tried. One answer currently posted is not a filter, and the other only negates 0 values - it doesn't do a greater than comparison.
    – Marc Kline
    Jun 6, 2014 at 12:20
  • I understand your comments, and your code is neat and simple.. Only reason i questioned it because i 'expected' Angular to have a 'one or two word' way of doing it.
    – Oam Psy
    Jun 6, 2014 at 12:40
  • 1
    I was trying to wonder how to do conditionals in filter. And this helped me alot. :) Sep 5, 2014 at 15:02
  • This solution is very elegant; thank you very much. Just learned a better way to do something.
    – Amir Mog
    Mar 25, 2017 at 19:57
44

You can create a filter with ng-if like this:

<li ng-repeat="seller in sellers" ng-if="seller.sales > 0" >{{ seller.name }}</li>
3
  • I personally prefer this approach rather than using the filters as it gives a bit more flexibility. Apr 4, 2016 at 15:31
  • 5
    This is easy to implement but becomes annoying when we rely on ng-repeat $index integrity, for example if $first / $last / etc doesn't have seller.sales then we can't apply special markup which is a common use case for me.
    – irth
    Jul 5, 2016 at 23:47
  • I know this approach is better than creating a custom filter.Now how can I get the count of sellers after ng-if applied in template? Nov 8, 2017 at 6:00
2
<tr ng-repeat-start="Data.Items in list = ( Data.Item | filter:{NumberOfStamps : !0}">
   <td><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
   <td>(Date of Birth {[{list.Dob}]})</td>
   <td>{[{list.NumberOfStamps}]}  stamps</td>
</tr>
4
  • In your post youve got Data.Items and Data.Item... Is it Item or Items??
    – Oam Psy
    Jun 6, 2014 at 12:07
  • My mistake: Data.Items
    – Dinesh ML
    Jun 6, 2014 at 12:20
  • 7
    !0 will return a false instead of a number, so won't work for numbers Jan 19, 2015 at 14:37
  • | filter:{ NumberOfStamps: '!0' } works with numbers.. some issues but works
    – oCcSking
    Oct 24, 2018 at 11:04
1

One possible way would be to remove the items that do not meet the criterai from the DOM using ng-if

<tr ng-repeat-start="list in Data.Items ">
    <td ng-if="list.NumberOfStamps > 0"><a href=" {[{list.Title}]} {[{list.ForeName}]} {[{list.SurName}]}</a></td>
    <td ng-if="list.NumberOfStamps > 0">(Date of Birth {[{list.Dob}]})</td>
    <td ng-if="list.NumberOfStamps > 0">{[{list.NumberOfStamps}]}  stamps</td>
</tr>

Because you cannot have a div in a tr you have to ng-if the td's seperately, which is not optimal if you have alot of td's

3
  • I think there is an extra or miss }} in your ng-if
    – Oam Psy
    Jun 6, 2014 at 12:11
  • This isnt working in my application. I think it has something to do with a DIV directly below a TR as thats incorrect MARKUP
    – Oam Psy
    Jun 6, 2014 at 12:21
  • 1
    @OamPsy you are correct, excuses. I edited my answer but Marc's solution is better
    – Tim
    Jun 6, 2014 at 12:31
0

Take a look at this stack overflow answer. AngularJS - How to structure a custom filter with ng-repeat to return items conditionally it clearly explains how to create a custom angular filter for use with ng-repeat. Once you understand this you will be able to filter out basically anything from an ng-repeat!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.