angularjs: how to get the index in ng-repeat - angularjs

I want to implement a pagination into my large ng-repeat, because the rendering of 200 objects is to slow.
I want to use the bootstrap-ui pagination.
Based on the selected page I will calculate which object will be displayed.
But in my ng-repeat i have no index. So my initial plan doesn't work. for example:
<div ng-if="index >= min && index <= max">
... show this object...
</div>
Can someone help me how to solve this problem?
thank you!

Angularjs provides $index on local scope of each template instance - iterator offset of the repeated element (0..length-1). https://docs.angularjs.org/api/ng/directive/ngRepeat
<div ng-repeat="(key, value) in myObj">
<div ng-if="$index >= min && $index <= max">
... show this object...
</div>
</div>

Related

Display size of ng-repeat with filter

I am currently working on an analytics backend web app and am using ng-repeat to show a table for some data I am working with. Is there a way to show the number of total results found when using a filter with ng-repeat? I would like to display this beneath my table.
You can retrieve the filtered data in a var like this :
<div ng-repeat="product in filteredProducts = (products | filter:search)">
So inside your ng-repeat you can do something like filteredProducts.length
You can use the $last variable of ngRepeat
something like this inside your ng-repeat would do the trick:
<div ng-if="$last"> Total results found: {{ $index + 1 }} </div>
note that you should add 1 to the $index since it starts at 0

Angularjs & Bootstrap - ng-repeat list filling

Actually, I have an array of cars with several information. I want to display the data as following:
I am using ng-repeat with % operator to determine the position, but the problem is that $index % 4 == 0 is also true for $index % 2 == 0 if for example $index is 8 or 16..
How can i solve this?
If you are using bootstrap you just need to use bootstrap smartly to have this kind of view
This is how you can do it:
<div class="row">
<div class="col-md-3" data-ng-repeat="car in cars">
{{ car.name }}
</div>
<div>

How to get previous object in ng-repeat after orderBy is applied

I have an array of objects that have a "Category" property. In my ng-repeat, I am creating a group by conditionally showing a header row with the category name if it is different that the previous value by using items[$index - 1] to check the previous value. This works, but if I apply an orderBy filter, it would seem that $index doesn't reflect the new order. What am I doing wrong?
As soon as I posted this, I realized what might fix it. I added track by item.itemId and it started acting as expected.
<div class="row" ng-repeat="item in items track by item.itemId | filter: searchTerms | orderBy:['Category1', 'Category2']">
<div class="rowHeader" ng-show="$index == 0 || item.Category1 != items[$index -1].Category1">
{{item.Category1}}
</div>
</div>

Angularjs ng-repeat index

Is there a way I can get ng-repeat current record index? I am trying to give each record div an id equal to the record index example:
<div id="1">
<div id="2">
<div id="3">
Any example is highly apreciated.
The ng-repeat i am using is as follows:
<div ng-repeat="rows in clients.Result">
Use $index with evaluation curls:
<div id="{{$index}}" ng-repeat="rows in clients.Result">
PLNKR
$index contains the current index. So you can use it inside your ng-repeat like {{$index}}.
All these variables are documented at the very top of the ng-repeat documentation.

condition / directive in Angular ng-repeat

I'm just starting out with angular. I currently have an json array of products being displayed within an ng-repeat and am looking to add a class to every third product.
Is there a way to write a condition within the repeater that will add a class to the html element when the index%3 is 2
Thanks
Sure!
<div ng-repeat='item in items' ng-class='{third: $index%3==2}'>{{item}}</div>
http://plnkr.co/edit/24Ne1Cyh71INYIKxBCcc
Yes, use the ng-class directive:
<div ng-repeat="item in items" ng-class="{ 'third-elm': $index%3 == 2 }">{{item.value}}</div>
jsFiddle: http://jsfiddle.net/bmleite/dKjz5/

Resources