如何使用ng-repeat迭代2个范围

时间:2017-05-22 11:20:59

标签: angularjs angularjs-scope angularjs-ng-repeat

如果我有两个共享一个公共人员代码值的范围,我可以在视图中使用ng-repeat链接它们吗?我想我想要做的是类似于嵌套for循环。 我想迭代一个范围,同时检查另一个范围是否匹配人员代码,然后如果人员代码存在于第二范围内,则显示不同的按钮。

<div ng-repeat="x in model.data | filter: cc.course_code | filter: occ.occurrence | filter: tgn.tutor_group_name | orderBy: 'surname'  | unique: 'PERSON_CODE'" ng-model="x">
  <h4><b>{{ x.forename }} {{x.surname}}</b>  ID: {{x.PERSON_CODE}}</h4>
  <h5>{{x.course_name}}</h5>
  <h5>{{ x.faculty_name }} {{x.area_code}}   </h5>
  <h5>{{x.area_name}}</h5>
  <h5>{{x.tutor_group_name}}</h5>
  <div ng-repeat="d in destinations.data" ng-model="d">
    <div ng-if="(x.PERSON_CODE === d.PERSON_CODE)">
      <div ng-show="d.EMP_DEST_CODE" class="pull-right">
        <a href="#myModal" type="button" class="btn btn-success butt-sz ladda-button" data-target="#myModal" data-toggle="modal" ng-click="editStudent(x.PERSON_CODE);">
          <span class="glyphicon glyphicon-pencil"></span> Edit Destination
        </a>
      </div>
    </div>
  </div>
  <div ng-show="(!d.EMP_DEST_CODE)" class="pull-right">
    <a href="#myModal" type="button" class="btn btn-warning butt-sz ladda-button" data-target="#myModal" data-toggle="modal" ng-click="editStudent(x.PERSON_CODE);">
      <span class="glyphicon glyphicon-plane"></span> Record Destination
    </a>
  </div>
</div>

我尝试了上述方法,但这根本不起作用。

如果目的地范围内有相应的条目,并且没有显示绿色按钮的Id,我希望显示绿色按钮。

1 个答案:

答案 0 :(得分:2)

我建议在控制器中编写一个函数来检查传递的条目是否在destinations中,如下所示:

$scope.isDestination = function(p) {
    for(var i = 0; i < $scope.destinations.data.length; i++) {
        if(p.PERSON_CODE == $scope.destinations.data[i].PERSON_CODE) 
            return true;
    }
    return false;
}

考虑到此功能,您可以有条件地显示您的按钮,而无需重复destinations数组:

<div ng-repeat="x in model.data | filter: cc.course_code | filter: occ.occurrence | filter: tgn.tutor_group_name | orderBy: 'surname'  | unique: 'PERSON_CODE'" ng-model="x">
  ...

  <div ng-if="isDestination(x)"> <!-- isDestination returns true -->
    <a href="#myModal" type="button" class="btn btn-success butt-sz ladda-button" data-target="#myModal" data-toggle="modal" ng-click="editStudent(x.PERSON_CODE);">
      <span class="glyphicon glyphicon-pencil"></span> Edit Destination
    </a>
  </div>

  <div ng-if="!isDestination(x)"> <!-- isDestination returns false -->
    <a href="#myModal" type="button" class="btn btn-danger butt-sz ladda-button" data-target="#myModal" data-toggle="modal" ng-click="editStudent(x.PERSON_CODE);">
      <span class="glyphicon glyphicon-plane"></span> Record Destination
    </a>
  </div>
</div>