AngularJS ng-repeat 处理空列表案例 [英] AngularJS ng-repeat handle empty list case

查看:22
本文介绍了AngularJS ng-repeat 处理空列表案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为这是一件很常见的事情,但我找不到如何在 AngularJS 中处理它.假设我有一个事件列表并想用 AngularJS 输出它们,那么这很简单:

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>

但是当列表为空时我该如何处理呢?我想在列表中放置一个消息框,其中包含无事件"或类似内容.唯一接近的是带有 events.lengthng-switch(当对象而不是数组时,我如何检查是否为空?),但是真的是我唯一的选择吗?

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?

推荐答案

您可以使用 ngShow.

<li ng-show="!events.length">No events</li>

参见示例.

或者你可以使用 ngHide

<li ng-hide="events.length">No events</li>

参见示例.

对于对象,您可以测试 Object.keys.

For object you can test Object.keys.

这篇关于AngularJS ng-repeat 处理空列表案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆