如果另一个DOM可见,我将如何显示项目?

时间:2015-01-11 18:03:09

标签: angularjs

我是否只需将ng-model添加到可见/不可见的DOM中,然后ng-show =" thatElement.visiblity ===' true'"?

<span>Show this text when the div is visible</span>
<div ng-repeat="user in users" ng-show="user.section===radio.model">{{user.name}}</div>

更新:如果我在div显示时显示此文本,则会为用户中的每个用户重复此操作。

1 个答案:

答案 0 :(得分:1)

你需要重组它:

<div ng-repeat="user in users" ng-show="user.section===radio.model">
    <span>Show this text when the div is visible</span>
    <div>{{user.name}}</div>
</div>

或者,(因为它不清楚),如果您希望div始终显示,并且仅在条件满足时跨越,则可以执行此操作:

<div ng-repeat="user in users">
    <span ng-show="user.section===radio.model">Show this text when the div is visible</span>
    <div>{{user.name}}</div>
</div>

您还可以使用过滤器来整理输入ng-repeat的对象,这样就可以避免处理不需要的子对象。


以下是在跨度上使用ng-if的简单示例:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.users = [{
      name: 'Mr. Visible',
      visibility: true
    }, {
      name: 'Mr. Invisible',
      visibility: false
    }]
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <div ng-repeat="user in users">
    <span ng-if="user.visibility">Show this text when the div is visible</span>
    <div>{{user.name}}</div>
    <br />
  </div>
</div>


更新

使用布尔值表示是否显示单个范围的示例:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.users = [{
      name: 'Mr. Visible',
      visibility: true
    }, {
      name: 'Mr. Invisible',
      visibility: false
    }];

    $scope.showSpan = false;
    for (var i in $scope.users) {
      if ($scope.users[i].visibility) {
        $scope.showSpan = true;
        break;
      }
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <span ng-show="showSpan">Show this text when the div is visible</span>
  <div ng-repeat="user in users">
    <div>{{user.name}}</div>
    <br />
  </div>
</div>