1

I am learning angular.js and trying to determine what the easiest way would be for me would be to display message 'no values found' when array is empty.

I have tried this which is not working

 <ul>
        <li ng-show="!items.length">No values</li>
        <li ng-repeat="i in items | searchFor:searchString">                
            <p>{{i.title}}</p>
        </li>
    </ul>

And my controller

app.filter('searchFor', function () {
return function (arr, searchString) {
    if (!searchString) {
        return arr;
    }
    var result = [];
    searchString = searchString.toLowerCase();
    angular.forEach(arr, function (item) {
        if (item.title.toLowerCase().indexOf(searchString) !== -1) {
            result.push(item);
        }
    });
    console.log(result.length);
    return result;
}
});

How could I display the message 'No values found' when there are no values returned?

2 Answers 2

5

Use filtered array in ng-if or ng-show

<ul>
    <li ng-repeat="i in filteredItems = (items | searchFor:searchString)">                
        <p>{{i.title}}</p>
    </li>
    <li ng-show="!filteredItems.length">No values</li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

This works:

<ul>
     <li ng-show="!(items | searchFor:searchString).length">
           No values
     </li>
     <li ng-repeat="i in items | searchFor:searchString">                
            <p>{{i.title}}</p>
     </li>
</ul>

Try the JSFiddle

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.