简体   繁体   English

单击时更改 ng-class(嵌套 ng-repeat)

[英]Changing ng-class when clicked (nested ng-repeat)

This is my angular code.. (to describe the problem I'm getting is that the nested ng-repeat for buttons repeats is behavior (when clicked) to all the "jobs" listed..这是我的角度代码..(描述我遇到的问题是按钮的嵌套 ng-repeat 重复是对列出的所有“作业”的行为(单击时)..

    <div class="row" ng-repeat="job in jobs">
        <div class="btn-group btn-group-justified" role="group">
             <div class="btn-group" role="group" ng-repeat="status in job.statuscollection">
                   <button type="button" class="btn btn-default" ng-class="{ 'btn-info': $index == selectedIndex }" ng-click="itemClicked($index)">{{status.name}}</button>
             </div>
        </div>
    </div>

Here's my js code..这是我的js代码..

    $scope.jobs = [
    {
        _id: new Date().toISOString(),
        statuscollection: [
            { name: "Off-Site"},
            { name: "Enroute" },
            { name: "On-Site" },
        ]
        docType: "job",
    },
    {
        _id: new Date().toISOString(),
        statuscollection: [
            { name: "Off-Site"},
            { name: "Enroute" },
            { name: "On-Site" },
        ]
        docType: "job",
    }];

This here's my ng-click function..这是我的 ng-click 功能..

    $scope.itemClicked = function ($index) {
         $scope.selectedIndex = $index;
         console.log($scope.jobs[$index]);
    }

I have more than just one job, I included only one here into the code to have less of it.我有不止一份工作,我在代码中只包含了一份工作以减少它。 but when the browser generates this data in the correct way, clicking on one of the "job's" buttons does the same thing for each job.但是当浏览器以正确的方式生成这些数据时,单击“作业”按钮之一会对每个作业执行相同的操作。

Meaning, if I click the button "on-site" for one job, it is duplicated for all the jobs.. how can I make it so that clicking just one of the job's buttons does it only for that job, and not all of them?意思是,如果我点击一项工作的“现场”按钮,它会为所有工作重复......他们?

Use $parent.$index instead of $index to refer to the outer ng-repeated loop (the job loop, that is):使用$parent.$index而不是$index来引用外部 ng-repeated 循环(即作业循环):

<button type="button" class="btn btn-default" ng-class="{ 'btn-info': $parent.index == selectedIndex }" ng-click="itemClicked($parent.$index)">{{status.name}}</button>

$index is the index of the inner loop. $index是内循环的索引。

$parent.$index is the index of the outer loop. $parent.$index是外循环的索引。

The proper Angular approach to track indices across multiple ngRepeats is to use ngInit to create a local variable that aliases $index for each instance of ngRepeat .跨多个ngRepeats跟踪索引的正确 Angular 方法是使用ngInit创建一个局部变量,为ngRepeat每个实例别名$index Using $parent to acquire its $index position is generally discouraged.通常不鼓励使用$parent来获取其$index位置。

And in the example below, we have two ngRepeats and therefore two aliased index variables: outerIndex and innerIndex :在下面的示例中,我们有两个ngRepeats ,因此有两个别名索引变量: outerIndexinnerIndex

  <tbody ng-repeat="country in countries" ng-init="outerIndex = $index">
    <tr ng-repeat="city in country.cities" ng-init="innerIndex = $index">
      <td>{{city}}</td>
      <td>{{country.name}}</td>
      <td>({{outerIndex}}, {{innerIndex}})</td>
    </tr>
  </tbody>

Plunker : http://plnkr.co/edit/VA1XWWrG3pghEcWli06F普朗克http : //plnkr.co/edit/VA1XWWrG3pghEcWli06F


Final Edit最终编辑

After understanding more of the context behind the question, it actually makes more sense for the OP to set an isActive property on the selected object instead attempting to track and match indices.在了解更多问题背后的上下文后,OP 在所选对象上设置isActive属性而不是尝试跟踪和匹配索引实际上更有意义。 That said the approach noted above is still valid and applicable for tracking indices across multiple ngRepeats也就是说,上面提到的方法仍然有效并且适用于跟踪多个ngRepeats索引

 $scope.itemClicked = function (status, job) {
   if (status.isActive) {
     status.isActive = false;
   } else {
     angular.forEach(job.statuscollection, function(status) {
       status.isActive = false;
     });
     status.isActive = true;
   }
 }

Updated plunker: http://plnkr.co/edit/v70fY30PjoTXCrhOPVZB更新的 plunker: http ://plnkr.co/edit/v70fY30PjoTXCrhOPVZB

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

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