1

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.

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.