使用angularJS ng-repeat中列表的一部分

时间:2014-07-23 06:40:47

标签: angularjs

我正在进行angularJS数据绑定,如下所示:

<div class="timeSlotWrapper">
<div class="timeSlotItem" ng-repeat="t in timeSlots" time-slot-obj="t" id ="{{t.id}}"
     ng-click="timeSlotClick(cardId, $index)">{{ t.signalingTimeSlot}}</div>
</div>

收集时间段包含大约60个项目,每个项目30个属于一个类别。让我们说typeId为0表示30表示,1表示其他30表示。我想仅对前30个使用ng-repeat。是否可以在ng-repeat中进行,或者我是否必须根据代码背后的需要创建集合?

2 个答案:

答案 0 :(得分:2)

<div class="timeSlotWrapper">
<div class="timeSlotItem" ng-repeat="t in timeSlots | filter:{typeId:0}" time-slot-obj="t" id ="{{t.id}}"
     ng-click="timeSlotClick(cardId, $index)">{{ t.signalingTimeSlot}}</div>
</div>

答案 1 :(得分:1)

您可以使用角度滤镜。相同的例子

myApp.filter('filterList', function () {
    return function(id) {
        if(id==1)
           return id;
    }
});

在你的HTML标记中

<div class="timeSlotItem" ng-repeat="t in timeSlots | filterList:t.id" time-slot-obj="t" id ="{{t.id}}"
 ng-click="timeSlotClick(cardId, $index)">{{ t.signalingTimeSlot}}</div>

<强>更新

如果1不需要硬编码,则可以在过滤器中使用$ scope对象:

myApp.filter('filterList', function () {
    return function($scope) {
       $scope.Objs.forEach(function(Obj){
        if(id==$scope.Obj.id) {
           return id;
           }
       });
    }

});

并在html标记中传递此对象

<div class="timeSlotItem" ng-repeat="t in timeSlots | filterList:this" time-slot-obj="t" id ="{{t.id}}"
     ng-click="timeSlotClick(cardId, $index)">{{ t.signalingTimeSlot}}</div>

Documentation on Angular Filters

相关问题