Angular js - 如果ng-repeat为空,则显示消息

时间:2014-09-16 10:15:40

标签: javascript angularjs

以下AngularJS应用程序正在使用ng-repeat和应用过滤器。某个应用的过滤器不会留下任何值。如何显示通知?

js fiddle

HTML

<div >
<div data-ng-controller="myCtrl">


    <ul >
        <li data-ng-repeat="item in values | filter:filterIds()"> 
            <code>#{{item.id}}</code> Item
        </li>
    </ul>

    <p ng-show="!values.length">no vals with this filter</p>
    <button ng-click="loadNewFilter()"> filter now</button>
</div>


</div>

AngularJS

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
$scope.values = [{
    id: 1
}, ....
}];

$scope.filter = [1,2,3,4,5,6];

$scope.filterIds = function (ids) {
        return function (item) {
            var filter = $scope.filter;

                 return filter.indexOf(item.id) !== -1;         
        }
 }

$scope.loadNewFilter = function (){
    $scope.filter = [-1];
    $scope.$apply();
}

});

7 个答案:

答案 0 :(得分:39)

我认为这就是你想要的:

var app = angular.module('myApp', []);    
app.controller('myCtrl', function ($scope) {
  $scope.values = [
    {id: 1}, 
    {id: 2}, 
    {id: 3}, 
    {id: 4}, 
    {id: 5}, 
    {id: 6}];
    
  $scope.filter = [1,2,3,4,5,6];
        
  $scope.filterIds = function (ids) {
    return function (item) {
      var filter = $scope.filter; 
      return filter.indexOf(item.id) !== -1;       	
    }
  }
        
  $scope.loadNewFilter = function (){
    $scope.filter = [1,5];    
  }                 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div data-ng-controller="myCtrl">               
    <ul>
      <li data-ng-repeat="item in testValue=(values | filter:filterIds())"> 
        <code>#{{item.id}}</code> Item
      </li>
    </ul>           
    <p ng-show="!testValue.length">no vals with this filter</p>
    <button ng-click="loadNewFilter()"> filter now</button>
  </div>
</div>

FIDDLE LINK

This is Another one FIDDLE LINK check this also

答案 1 :(得分:28)

我会选择非常简单的CSS方法:

HTML:

<ul>
    <li data-ng-repeat="item in values | filter:filterIds()"> <code>#{{item.id}}</code> Item</li>
    <li class="no-items">There are no matching items.</li>
</ul>

CSS:

li + .no-items {
    display: none;
}

所以基本上li.no-items只有在没有其他LI的情况下才会显示,否则会被隐藏。我认为这比使用ngShow/ngHide引入一个观察者更好。

演示:http://jsfiddle.net/z9daLbLm/5/

答案 2 :(得分:7)

以下是工作示例:http://jsfiddle.net/z9daLbLm/2/

您可以在ng-repeat

中保存过滤器的结果
<div ng-repeat="item in filteredValues = (values | filter:filterIds())">{{item}}</div>

结果存储在filteredValues 然后在DOM中使用此过滤值

<div ng-show="!filteredValues.length">No Items</div>

答案 3 :(得分:2)

请参阅此处http://jsfiddle.net/ntofav3c/

改变这个:

<p ng-show="!values.length">no vals with this filter</p>

为:

 <p ng-hide="values | filter:filterIds()">no vals with this filter</p>

答案 4 :(得分:2)

看到这个工作jsfiddle:http://jsfiddle.net/z9daLbLm/4/ 我在您的<ul>列表

之前添加了此代码
<div ng-if="(values|filter:filterIds()).length == 0">
    List is empty
</div>

它只显示&#34;列表为空&#34;过滤后的值长度为零时的文本。

有关详细信息,请参阅以下链接:How to display length of filtered ng-repeat data

答案 5 :(得分:0)

  

您可以使用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>`

答案 6 :(得分:0)

我最近遇到了同样的问题.. 我在ng-repeat中有三个过滤器和一个orderBy,想要显示一些空列表消息....
上面的解决方案没有用。所以我自己做了一个。

  • 将emptymsg设为背景div,将mainContainer设为top div ..
  • 将始终显示emptyMsg div,因此每当列表中没有值时,emptyMsg将是唯一显示的div。 PS:代码片段不会运行..因为没有添加额外的库..这只是为了给你一个想法!
    &#13;
    &#13;
    //empty msg....
    .emptyMsg{
        position: absolute;
        left: 0;right: 0;
        margin: auto;
        color: gray;
        text-align: center;
        font-size:3.0em;
        z-index: 0;
    }
    .activityItem {
        margin: 0px !important;
        padding: 0px !important;
        border: 0px !important;
    }
    .mainContainer {
        z-index: 999;
        width: 100%;
        height: 40px;
        left: 0px;
        margin-top: 10px;
        display: inline-block;
    }
    &#13;
    <h4>List of Activities</h4>
            <ion-list>
               <div class="emptyMsg">No Activities found!</div>
                <div class="item activityItem" ng-repeat="activity in activities | orderBy:field1Order:order.reverse1 | filter:importanceFilter | filter:motiveFilter track by $index">
                       <div class="mainContainer">
    
                                <div class="divBar">{{activity.energy}}%</div>
                       
                    </div>
                </div>
                
            </ion-list>
    &#13;
    &#13;
    &#13;