简体   繁体   English

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

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

QUESTION

Using the above question's code, my question is there a clean way to be able to remove all the booleans in each object and get angular to know whether to show/hide each individual repeated object. 使用上面的问题的代码,我的问题是有一种干净的方法,能够删除每个对象中的所有布尔值,并使角度确定是否显示/隐藏每个重复的对象。

I have a table of items that if you click a button it will flip a boolean and show it's self, but it will do this to every item in the table and not just the individual item as they are all pointing to the same boolean. 我有一个项目表,如果您单击按钮,它将翻转一个布尔值并显示其自身,但是它将对表中的每个项目执行此操作,而不仅仅是单个项,因为它们都指向同一个布尔值。

Is there a way to dynamically generate a boolean for each item? 有没有一种方法可以为每个项目动态生成布尔值? I really don't want to have to add a show item within each object to call on. 我真的不想在每个对象中添加一个显示项来调用。 There has to be a better way. 一定有更好的方法。 Edit: Here is a better template to use as an example: JSFiddle 编辑:这是一个更好的模板用作示例: JSFiddle

HTML 的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 的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,
  }];
});

Try this in context with question you mentioned 在您提到的问题中尝试这种情况

In your html pass 在您的html pass中

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

While in js 在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