Angularjs ng-repeat handle empty list case
Display a fallback message for an empty list in AngularJS with ng-if
:
This checks if myList
is empty and shows a message, preventing extra rendering when items exist.
Nailing the basics
When dealing with an empty list, consider these basic strategies:
- Display an empty list message: Use
ng-show="!myList.length"
to show a message if the list is empty. - Hide message when not empty: Define
ng-hide="myList.length"
on your message to hide it when the list has items.
Got an empty object? No problem! Check its emptiness with Object.keys(myObject).length === 0
.
Show loading states
Indicate a loading state when fetching data asynchronously:
Filtering and customized messaging
Applying filters to a list and displaying corresponding messages can enhance UX:
Going above and beyond
Tweak UX in empty states
Creating a seamless user experience, despite an empty list, is vital:
- Dynamic placeholders: Display placeholders to indicate that data is being fetched.
- Graceful degradation: Inform users if the list is intentionally empty.
- Contextual Feedback: Customize the empty state message to the specific situation.
Improve efficiency
Using ng-if
can prevent unnecessary DOM manipulations. It adds or removes elements only when necessary:
Filtering and conditional display
Implementing ng-hide
based on the length of the filtered list makes lists responsive to search filters:
Custom AngularJS filters
Custom filters can clarify list handling and can be combined with ng-show
or ng-hide
:
Then, apply it:
Was this article helpful?