Ivan Xavier Santirachi
Ivan Xavier Santirachi

Reputation: 179

How to get actual value from array filtered in angularJS

My problem is that I need to know when my array filtered is empty. I'm applying some filters to an array and showing the results, when the filters make no results, I need to show a message error. How can I catch the moment when the array is empty ?

Html:

<div ng-repeat="array in arrays | filterArray1 | filterArray2 | filterArray3 | filter: 'name'>

Thanks in advance.

Upvotes: 1

Views: 1045

Answers (2)

Bastien Caudan
Bastien Caudan

Reputation: 4490

You can assign your filtered array to a variable and then display the message :

<div ng-repeat="array in filteredArrays = (arrays | filterArray1 | filterArray2 | filterArray3 | filter: 'name')>
   ...
</div>
<div ng-show="filteredArrays.length == 0">Empty message</div>

Upvotes: 5

Mathew Berg
Mathew Berg

Reputation: 28750

You can use ng-init to store it in a temporary variable:

<div ng-app="app">
    <div ng-controller="ParentCtrl">
        <div data-ng-init="filteredArray = (arrays | myFilter)">
            <div data-ng-repeat="data in filteredArray">
                {{ data }}
            </div>
            <div data-ng-show="!filteredArray.length">
                no results
                <!-- no results -->
            </div>
        </div>
    </div>
</div>

Here's a fiddle that has a filter that manipulates the data: http://jsfiddle.net/XwFQJ/

Upvotes: 0

Related Questions