如何ng - 如果另一个div是可见的

时间:2016-12-06 00:36:49

标签: javascript html angularjs

我有两个使用ng-repeat的东西列表。然后我想要它说"空"如果列表中没有项目。

这是更新版本:

     <!-- First List -->
        <div ng-repeat="request in requests" ng-if="request.condition1 == 'something'">
            {{request}}
        </div>
        <div>Empty list</div>

     <!-- Second List (both use requests array but have different conditions-->
        <div ng-repeat="request in requests" ng-if="request.condition22222 == 'something else'">
            {{request}}
        </div>
        <div>Empty list</div>

2 个答案:

答案 0 :(得分:1)

试试这个:

&#13;
&#13;
(function(angular) {
  'use strict';
angular.module('app', [])
  .filter('decorate', function() {    
  return function(input, model) {   
    return input.filter(function(val){      
      return val[model.prop] == model.val;
    });    
  };
}).controller('MyController', ['$scope', '$filter', function($scope, $filter) {
    $scope.requests = [{name:'Tom', age:23},{name:'Henry', age:23},{name:'Max', age:33}];    
    $scope.$filter = $filter;
  }]) 
})(window.angular);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="MyController">

  <div ng-init='items1=$filter("decorate")(requests, {prop:"age", val:23})'>First list(age=23):</div>
  <div ng-repeat='item in items1' ng-if='items1.length > 0' >{{item | json}}</div>
  <div ng-if='items1.length == 0'>Empty</div>

  <br/>  

  <div ng-init='items2=$filter("decorate")(requests, {prop:"name", val:"Sam"})'>Second list(name=Sam):</div>
  <div ng-repeat='item in items2' ng-if='items2'>{{item | json}}</div>
  <div ng-if='items2.length == 0'>Empty</div>

</div>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

您可以检查列表的长度并有条件地显示:

{{1}}

如果您正在使用某个对象,则可以使用Object.keys,但某些较旧的浏览器可能不支持此功能。