I thought this would be a very common thing, but I couldn't find how to handle it in AngularJS. Let's say I have a list of events and want to output them with AngularJS, then that's pretty easy:
<ul>
<li ng-repeat="event in events">{{event.title}}</li>
</ul>
But how do I handle the case when the list is empty? I want to have a message box in place where the list is with something like "No events" or similar. The only thing that would come close is the ng-switch
with events.length
(how do I check if empty when an object and not an array?), but is that really the only option I have?
You might want to check out the angular-ui directive
ui-if
if you just want to remove theul
from the DOM when the list is empty:This is similar to @Konrad 'ktoso' Malawski but slightly easier to remember.
Tested with Angular 1.4
You can use this ng-switch:
you can use ng-if because this is not render in html page and you dont see your html tag in inspect...
You can use ngShow.
See example.
Or you can use ngHide
See example.
For object you can test Object.keys.
And if you want to use this with a filtered list here's a neat trick: