单击元素时隐藏兄弟元素

时间:2015-04-17 20:43:33

标签: javascript angularjs angularjs-ng-repeat

ng-repeat中,当我点击一个元素时,我试图隐藏一个兄弟元素。在我的controllerClickMethod内,我会检查item.attribute是否等于data-group中的属性值。我怎么能做到这一点?

<div ng-repeat="item in itemArray">
    <div ng-click="controllerClickMethod(item)">
        <h4>
            Label
        </h4>
    </div>
    <div data-group="{item.attribute}"></div>
</div>

1 个答案:

答案 0 :(得分:0)

这样的东西会起作用。将ng-clickng-hide指令添加到元素中。在ng-click中,引入一个将hide属性添加到下一个兄弟的函数。

&#13;
&#13;
angular.module('HidingPeeps', [])
.controller('Peeps', function() {
  this.peeps = [
    {name: 'Joe'},
    {name: 'Bob'},
    {name: 'Sarrah'},
    {name: 'Tom'},
    {name: 'You'}
  ];
  this.hide = function ($index) {
    if(!this.peeps[$index]) {
      $index = 0;
    }
    this.peeps[$index].hide = true;
  }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="HidingPeeps">
     <div ng-controller="Peeps as C">
       <div ng-repeat="peep in C.peeps" ng-click="C.hide($index+1)" ng-hide="peep.hide">
         {{peep.name}}
       </div>
     </div>
</div>
&#13;
&#13;
&#13;