当子元素为空时隐藏父元素在DOM中的显示

3
<div>
  <h1>Birds</h1>
  <ul>
    <li ng-if="bird.type === 'bird'"
        ng-repeat="bird in creatures">{{bird.name}}</li>
  </ul>
</div>

我从服务器收到了响应,我想把它放进列表里。但是当列表为空时,我需要隐藏这个div容器。例如,如果数组中没有bird.type ==='bird' - 我想要隐藏div。但是我想在ng-repeat之后使用鸟(bird),所以我不能在div上使用ng-if="bird.type === 'bird'"。我能检查li是否为空,然后隐藏div吗? plunkr 示例 AngularJS ng-repeat处理空列表情况 - 这不是同一个问题。我不想在li为空时隐藏它,我想在li为空时隐藏包含h1的父元素div。

你是在问如何在creatures.length == 0的情况下隐藏div吗?我不确定我理解你的问题。 - frosty
@frosty 我想要在 bird.type 是 null 的时候隐藏 div。例如,我有 .type === 'bird',.type === 'dog' 和 .type === 'cat',但我没有 'fish',所以如果 fish.type === null - li 将是空的。但是 div 和 h3 会显示出来。 - YoroDiallo
@T.J. Crowder 你可以在 plunkr 中看到,如果我没有某些元素,li 将是空的,但 h3 和 div 不会。 - YoroDiallo
那么你想隐藏鱼的div吗? - frosty
1
请看我的答案。如果没有该类型的动物,它有一种隐藏该部分的方法。 - frosty
5个回答

4
您可以采取以下措施:
<div ng-if="hasBirds(creatures)">
  <h1>Birds</h1>
  <ul>
    <li ng-if="bird.type === 'bird'"
        ng-repeat="bird in creatures">{{bird.name}}</li>
  </ul>
</div>

然后在控制器或指令中,您可以添加 hasBirds 函数。

$scope.hasBirds = function(list){
    return list.filter(function(item){return item.type === 'bird'}).length > 0;
}

这个hasBirds函数经常被调用,但是它可以让你隐藏/显示鸟的标题。


2

在您的例子中,我建议您使用过滤器而不是使用“ng-if”,您应该创建一个类似如下的过滤器:

angular.module('moduleName').filter(birdsFilter);
function birdsFilter(creature) {
    return creature.type == 'bird';
}

使用筛选器,您可以将代码重写为以下内容:
<div ng-hide="birds.length">
  <h1>Birds</h1>
  <ul>
    <li ng-repeat="bird in birds = (creatures | filter:birdsFilter)">{{bird.name}}</li>
  </ul>
</div>

2

在我看来,这些答案都可以实现目的。但是它们都没有最优化。我建议在你的控制器或postlink函数中过滤数据。

$scope.animals = {
    dogs: $scope.creates.filter(function(a){return a.type == 'dog'}),
    cats: $scope.creates.filter(function(a){return a.type == 'cat'}),
    birds: $scope.creates.filter(function(a){return a.type == 'bird'}),
    fishes: $scope.creates.filter(function(a){return a.type == 'fish'})
};

这样,您只需要在一个地方处理生物数组一次。摘要周期永远不必重新评估数组以查看是否需要更新DOM。那么,您的标记将如下所示:

<div ng-if="animals.birds.length">
  <h1>Birds</h1>
  <ul>
    <li ng-repeat="bird in animals.birds">{{bird.name}}</li>
  </ul>
</div>

1
使用ng-show="cats.length"来使div在长度为零时消失。
根据对象属性内联过滤,如cat in creatures | filter:{type: 'cat'} as cats,如this SO post所讨论。 工作示例:

var app = angular.module('App', []);
app.filter(birdsFilter);
function birdsFilter(creature) {
    return creature.type == 'bird';
}
app.controller('Ctrl', function($scope) {
  $scope.creatures = [
      {
        name : 'Cho-cho',
        type : 'bird'
      },
      {
        name : 'Floo-floo',
        type : 'dog'
      },
            {
        name : 'Pou-pou',
        type : 'bird'
      },
      {
        name : 'Oop-flup',
        type : 'bird'
      },
            {
        name : 'Chio-mio',
        type : 'cat'
      },
      {
        name : 'Floo-floo',
        type : 'dog'
      },
            {
        name : 'Loo-Li',
        type : 'dog'
      },
      {
        name : 'Pops-Mops',
        type : 'bird'
      },
            {
        name : 'Boo-Moo',
        type : 'dog'
      },
      {
        name : 'Iop-Pio',
        type : 'dog'
      },
            {
        name : 'Floop-cho',
        type : 'bird'
      }
      
    ]
});
<!DOCTYPE html>
<html ng-app="App">

<head>
  <script data-require="angularjs@1.5.7" data-semver="1.5.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="Ctrl">
  <div ng-show="birds.length">
    <h1>Birds</h1>
    <ul>
      <li ng-repeat="bird in creatures | filter:{type: 'bird'} as birds">{{bird.name}} </li>
    </ul>
  </div>
  <div ng-show="dogs.length">
    <h1>Dogs</h1>
    <ul>
      <li ng-repeat="dog in creatures | filter:{type: 'dog'} as dogs">{{dog.name}} </li>
    </ul>
  </div>
  <div ng-show="cats.length">
    <h1>Cats</h1>
    <ul>
      <li ng-repeat="cat in creatures | filter:{type: 'cat'} as cats">{{cat.name}} </li>
    </ul>
  </div>
  <div ng-show="fishes.length">
    <h1>Fish</h1>
    <ul>
      <li ng-repeat="fish in creatures | filter:{type: 'fish'} as fishes">{{fish.name}} </li>
    </ul>
  </div>

</body>

</html>


1
你应该根据类型过滤列表,将过滤后的项目存储在作用域属性中,然后使用该属性来显示或隐藏div。
<div ng-show="birds.length">
  <h1>Birds</h1>
  <ul>
    <li ng-repeat="bird in creatures | filter:birdType as birds">{{bird.name}}    </li>
  </ul>
</div>

然后在您的控制器中实现birdType筛选函数:

$scope.birdType = function(creature) {
    return creature.type === 'bird';
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接