Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-show when array length is zero

Tags:

angularjs

I am a beginner for AngularJS. I am trying to display "No Tag Found" during filter process with the help of "ng-show".

JS:

function simpleController($scope) { $scope.tags = ['HTML','CSS','Jquery','Bootstrap','AngularJS']; } 

HTML:

<div ng-controller="simpleController"> <input  class="txt" type="text" ng-model="nameText" />  <div>  <ul>   <li ng-repeat="myKeys in tags| filter:nameText">{{myKeys}}</li>  </ul> <div ng-show="!tags.length">No Tag Found</div> </div> </div> 

When I type any value other than array vales, I am not able to get "No Tag Found" using the above code. Please help. Thanks.

like image 586
user2218497 Avatar asked Apr 15 '14 06:04

user2218497


People also ask

How do I check if an array is empty in AngularJS?

We can also explicitly check if the array is empty or not. if (arr. length === 0) { console. log("Array is empty!") }

Which is better Ng-if or NG-show?

ng-if can only render data whenever the condition is true. It doesn't have any rendered data until the condition is true. ng-show can show and hide the rendered data, that is, it always kept the rendered data and show or hide on the basis of that directives.

How do you condition in NG-repeat?

You can add condition using ng-if also and this is effective too. You can apply any condition to your list using ng-if attribute. In below example, I have put condition where Age > 20 and IsActive is true. ng-repeat will fetch all records which full fill this scenario.

Is ng-show a directive?

AngularJS ng-show DirectiveThe ng-show directive shows the specified HTML element if the expression evaluates to true, otherwise the HTML element is hidden.


1 Answers

If you're filtering in your ng-repeat, you must apply the same filter for you ng-show. If you don't, the ng-show will always refer to the full array :

<div ng-show="!(tags| filter:nameText).length">No Tag Found</div> 

Working fiddle : http://jsfiddle.net/HB7LU/3149/

like image 81
Jscti Avatar answered Sep 19 '22 15:09

Jscti