繁体   English   中英

显示/隐藏ng-repeat中的项目

[英]Show/Hide items in an ng-repeat

使用上面的问题的代码,我的问题是有一种干净的方法,能够删除每个对象中的所有布尔值,并使角度确定是否显示/隐藏每个重复的对象。

我有一个项目表,如果您单击按钮,它将翻转一个布尔值并显示其自身,但是它将对表中的每个项目执行此操作,而不仅仅是单个项,因为它们都指向同一个布尔值。

有没有一种方法可以为每个项目动态生成布尔值? 我真的不想在每个对象中添加一个显示项来调用。 一定有更好的方法。 编辑:这是一个更好的模板用作示例: JSFiddle

的HTML

<body ng-app="task" ng-controller="repeat">
 <table class="table table-striped table-hover">
  <thead>
    <tr>
      <td>Account</td>
      <td>Details</td>
      <td>Secret</td>
      <td>Action</td>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="x in accounts">
      <td>{{x.account}}</td>
      <td>{{x.details}}</td>
      <td><span>{{x.secret}}</span></td>
      <td>
         <button type="button" class="btn btn-default">Show Secret</button>
      </td>
     </tr>
   </tbody>
 </table>
</body>

的JavaScript

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

app.controller('repeat', function($scope) {
  $scope.accounts = [{
    account: "account 01",
    details: "lorum ipsum",
    secret: 0101101,
  }, {
    account: "account 02",
    details: "lorum ipsum",
    secret: 0101101,
  }, {
    account: "account 03",
    details: "lorum ipsum",
    secret: 0101101,
  }, {
    account: "account 04",
    details: "lorum ipsum",
    secret: 0101101,
  }];
});

在您提到的问题中尝试这种情况

在您的html pass中

   <button ng-click="toggle(x.$index)">Hide</button>

在js中

  $scope.toggle = function(index){
  $scope.array[index].show = !$scope.array[index].show;
  };

在按钮上,调用angularjs函数设置布尔值,然后使用ng-show / ng-hide切换视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM