devC
devC

Reputation: 1444

using part of a list in angularJS ng-repeat

I'm doing an angularJS data-binding as follows:

<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>

the collection timeslots contain some 60 items, 30 of each belonging to one category. Lets say typeId is 0 for 30, and 1 for the other 30. I want to use ng-repeat for the first 30 only. Is it possible to do within ng-repeat or do I have to create the collection according to my need in code behind?

Upvotes: 0

Views: 271

Answers (2)

V31
V31

Reputation: 7666

You can make use of angular filter. Example for same

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

And in your html markup

<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>

UPDATE:

If 1 need not be hardcoded then a $scope object can be used in the filter:

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

});

and in html markup pass this object

<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

Upvotes: 1

micronyks
micronyks

Reputation: 55443

<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>

Upvotes: 2

Related Questions