miholzi
miholzi

Reputation: 1004

ng-repeat filter is empty loop

i do the following ng-repeat loop and it works well!

<div ng-repeat="einschItem in einschaetzungen.alldata | filter: { savedatum: lolatage[tagarrayindex].tagestring } | orderBy : '-savetimestamp'">

now i want to check if the filtered is empty, so i searched and found this way how it should solve my problem.

<div ng-repeat="einschItem in einschaetzungen.alldata = (values | filter: { savedatum: lolatage[tagarrayindex].tagestring } | orderBy : '-savetimestamp')">

{{values.length}}

But if use this code i don't get any results in the loop anymore, the "einschItem" ist empty.

thanks!

i use angular v1.4.6

Upvotes: 2

Views: 287

Answers (2)

Malkus
Malkus

Reputation: 3726

It looks like you have values and einshaetzungen.alldata in the wrong places

You want to assign the filtered list to a value then iterate through it.

<div ng-repeat="einschItem in values = (einschaetzungen.alldata| filter: { savedatum: lolatage[tagarrayindex].tagestring } | orderBy : '-savetimestamp')">

{{values.length}}

Here is a similar example

This version works for older versions of angular. For v1.3.17-beta+ you can use the alias formatting mentioned in Claies answer.

<div ng-repeat="einschItem in einschaetzungen.alldata | 
               filter: { savedatum: lolatage[tagarrayindex].tagestring } |
               orderBy : '-savetimestamp' 
               as filteredItems">

{{filteredItems.length}}

Upvotes: 1

Claies
Claies

Reputation: 22323

What you probably want here is to alias the results of the repeat. From the Angular Documentation:

variable in expression as alias_expression – You can also provide an optional alias expression which will then store the intermediate results of the repeater after the filters have been applied. Typically this is used to render a special message when a filter is active on the repeater, but the filtered result set is empty.

For example: item in items | filter:x as results will store the fragment of the repeated items as results, but only after the items have been processed through the filter.

Please note that as [variable name] is not an operator but rather a part of ngRepeat micro-syntax so it can be used only at the end (and not as operator, inside an expression).

For example: item in items | filter : x | orderBy : order | limitTo : limit as results.

So in your case, you can use:

<div ng-repeat="einschItem in einschaetzungen.alldata | 
               filter: { savedatum: lolatage[tagarrayindex].tagestring } |
               orderBy : '-savetimestamp' 
               as filteredItems">

{{filteredItems.length}}

Note that this is functionally the same as the syntax provided in the other answer to the question; This is just an alternative provided by the ngRepeat micro-syntax.

Upvotes: 3

Related Questions