Nageswar Nageswar - 2 months ago 13
AngularJS Question

Showing message in ng-repeat when search string array is empty in angular

<ul class="data-ctrl mCustomScrollbar" id="scrollbar" data-mcs-theme="minimal-dark dark123" data-ng-init="initScrollBar()">
<li data-ng-repeat="i in items | searchFor:searchString | limitTo:limit" >
<p><a data-ng-href="{{i.link}}" class="search-link">{{i.title}}</a></p>
<p>{{i.description}}</p>
<p><a class="small" data-ng-href="{{i.link}}">{{i.link}}</a></p>
<li data-ng-repeat-empty >No Records Found</li>
</li>

</ul>


I am providing search option to the user using above code.when i am trying to show message like no record found when search string is empty from my json db file using data-ng-repeat-empty, but problem is 'No Records Found' showing for both cases like Record found and record not found.

Answer

Store the filter data in variable and then check the length of variable it will work.

<ul>
    <li data-ng-repeat="i in filterItem = (items | searchFor:searchString | limitTo:limit)" >
       <p><a data-ng-href="{{i.link}}" class="search-link">{{i.title}}</a></p>
       <p>{{i.description}}</p>
       <p><a class="small" data-ng-href="{{i.link}}">{{i.link}}</a></p>        
    </li>
    <li ng-if="!filterItem.length">No Records Found</li> 
</ul>