显示/隐藏按钮onclick在重复内的ng-repeat中

时间:2018-11-01 22:47:05

标签: javascript angularjs angularjs-ng-repeat

如何在第二次重复中访问和显示/隐藏确切的按钮?

<div class="row" ng-repeat="person in people">
  <div class="row" ng-repeat="ticket in tickets">
   <button type="button" ng-if="!ticket.added" ng-click="add(ticket)">+</button>
   <button type="button" ng-if="ticket.added" ng-click="remove(ticket)">-</button>
  </div>
</div>

例如,我有3个人和4张不同的门票。当某人单击按钮时,我要添加该人的单击的票证。

现在,当我单击“添加”按钮时,它正在添加所有人的单击的凭单:(

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定(您的问题不太清楚),但是也许您也可以将 person 传递给您的功能?

示例:

$scope.people = [{
        name: 'Jhon Snow',
      tickets: [{
        name: 'ticket1',
        added: false
      }, {
        name: 'ticket2',
        added: false
      }]
    }, {
        name: 'Peter Parker',
      tickets: [{
        name: 'ticket3',
        added: false
      }, {
        name: 'ticket4',
        added: false
      }]
    }];    

    $scope.add = function (ticket) {
        ticket.added = true;      
    }

    $scope.remove = function (ticket) {
        ticket.added = false;
    }

还有html:

<div class="row" ng-repeat="person in people">
  {{ person.name}}
  <div class="row" ng-repeat="ticket in person.tickets">
    - {{ ticket.name }}
     <button type="button" ng-if="!ticket.added" ng-click="add(ticket)">+</button>
     <button type="button" ng-if="ticket.added" ng-click="remove(ticket)">-</button>
  </div>
</div>

您可以查看一个有效的示例here