Javascript AngularJS ng-repeat 处理空列表案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12340095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:45:12  来源:igfitidea点击:

AngularJS ng-repeat handle empty list case

javascriptangularjsangularjs-ng-repeatangularjs-ng-show

提问by Prinzhorn

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:

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

<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-switchwith events.length(how do I check if empty when an object and not an array?), but is that really the only option I have?

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

回答by Artem Andreev

You can use ngShow.

您可以使用ngShow

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

See example.

参见示例

Or you can use ngHide

或者你可以使用ngHide

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

See example.

参见示例

For object you can test Object.keys.

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

回答by Konrad 'ktoso' Malawski

And if you want to use this with a filtered list here's a neat trick:

如果你想将它与过滤列表一起使用,这里有一个巧妙的技巧:

<ul>
    <li ng-repeat="item in filteredItems  = (items | filter:keyword)">
        ...
    </li>
</ul>
<div ng-hide="filteredItems.length">No items found</div>

回答by Pylinux

With the newer versions of angularjs the correct answer to this question is to use ng-if:

对于较新版本的 angularjs,这个问题的正确答案是使用ng-if

<ul>
  <li ng-if="list.length === 0">( No items in this list yet! )</li>
  <li ng-repeat="item in list">{{ item }}</li>
</ul>

This solution will not flicker when the list is about to download either because the list has to be defined and with a length of 0 for the message to display.

当列表即将下载时,此解决方案也不会闪烁,因为必须定义列表并且长度为 0 才能显示消息。

Here is a plunker to show it in use: http://plnkr.co/edit/in7ha1wTlpuVgamiOblS?p=preview

这是一个显示它在使用中的plunker:http://plnkr.co/edit/in7ha1wTlpuVgamiOblS?p=preview

Tip: You can also show a loading text or spinner:

提示:您还可以显示加载文本或微调器:

  <li ng-if="!list">( Loading... )</li>

回答by Mortimer

You might want to check out the angular-ui directiveui-ifif you just want to remove the ulfrom the DOM when the list is empty:

如果您只想在列表为空时从 DOM 中删除 ,您可能需要查看angular-ui 指令ui-iful

<ul ui-if="!!events.length">
    <li ng-repeat="event in events">{{event.title}}</li>
</ul>

回答by Bernard

<ul>
    <li ng-repeat="item in items | filter:keyword as filteredItems">
        ...
    </li>
    <li ng-if="filteredItems.length===0">
        No items found
    </li>
</ul>

This is similar to @Konrad 'ktoso' Malawski but slightly easier to remember.

这类似于@Konrad 'ktoso' Malawski,但稍微容易记住。

Tested with Angular 1.4

用 Angular 1.4 测试

回答by Miriam Salzer

Here's a different approach using CSS instead of JavaScript/AngularJS.

这是一种使用 CSS 而不是 JavaScript/AngularJS 的不同方法。

CSS:

CSS:

.emptymsg {
  display: list-item;
}

li + .emptymsg {
  display: none;
}

Markup:

标记:

<ul>
    <li ng-repeat="item in filteredItems"> ... </li>
    <li class="emptymsg">No items found</li>
</ul>

If the list is empty, <li ng-repeat="item in filteredItems">, etc. will get commented out and will become a comment instead of a li element.

如果列表为空,<li ng-repeat="item infilteredItems"> 等将被注释掉并成为注释而不是 li 元素。

回答by LukitaBrands

You can use this ng-switch:

你可以使用这个 ng-switch:

<div ng-app ng-controller="friendsCtrl">
  <label>Search: </label><input ng-model="searchText" type="text">
  <div ng-init="filtered = (friends | filter:searchText)">
  <h3>'Found '{{(friends | filter:searchText).length}} friends</h3>
  <div ng-switch="(friends | filter:searchText).length">
    <span class="ng-empty" ng-switch-when="0">No friends</span>
    <table ng-switch-default>
      <thead>  
        <tr>
          <th>Name</th>
          <th>Phone</th>
        </tr>
      </thead>
      <tbody>
      <tr ng-repeat="friend in friends | filter:searchText">
        <td>{{friend.name}}</td>
        <td>{{friend.phone}}</td>
      </tr>
    </tbody>
  </table>
</div>

回答by Damian Czapiewski

You can use askeyword to refer a collection under a ng-repeatelement:

您可以使用as关键字来引用ng-repeat元素下的集合:

<table>
    <tr ng-repeat="task in tasks | filter:category | filter:query as res">
        <td>{{task.id}}</td>
        <td>{{task.description}}</td>
    </tr>
    <tr ng-if="res.length === 0">
        <td colspan="2">no results</td>
    </tr>
</table>

回答by Ezequiel García

i usually use ng-show

我通常使用 ng-show

<li ng-show="variable.length"></li>

where variable you define for example

例如,您定义的变量

<div class="list-group-item" ng-repeat="product in store.products">
   <li ng-show="product.length">show something</li>
</div>

回答by pejman

you can use ng-if because this is not render in html page and you dont see your html tag in inspect...

您可以使用 ng-if,因为这不会在 html 页面中呈现,并且您在检查中看不到 html 标签...

<ul ng-repeat="item in items" ng-if="items.length > 0">
    <li>{{item}}<li>
</ul>
<div class="alert alert-info">there is no items!</div>