Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 52b8211

Browse files
npupksheedlo
authored andcommitted
feat(ngRepeat): add $even and $odd props to iterator
1 parent 0fcd1e3 commit 52b8211

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/ng/directive/ngRepeat.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* | `$first` | {@type boolean} | true if the repeated element is first in the iterator. |
1818
* | `$middle` | {@type boolean} | true if the repeated element is between the first and last in the iterator. |
1919
* | `$last` | {@type boolean} | true if the repeated element is last in the iterator. |
20+
* | `$even` | {@type boolean} | true if the iterator position `$index` is even (otherwise false). |
21+
* | `$odd` | {@type boolean} | true if the iterator position `$index` is odd (otherwise false). |
2022
*
2123
* Additionally, you can also provide animations via the ngAnimate attribute to animate the **enter**,
2224
* **leave** and **move** effects.
@@ -354,6 +356,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
354356
childScope.$first = (index === 0);
355357
childScope.$last = (index === (arrayLength - 1));
356358
childScope.$middle = !(childScope.$first || childScope.$last);
359+
childScope.$odd = !(childScope.$even = index%2==0);
357360

358361
if (!block.startNode) {
359362
linker(childScope, function(clone) {

test/ng/directive/ngRepeatSpec.js

+62-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ describe('ngRepeat', function() {
353353
expect(element.text()).toEqual('misko:0|shyam:1|frodo:2|');
354354
});
355355

356-
357356
it('should expose iterator offset as $index when iterating over objects', function() {
358357
element = $compile(
359358
'<ul>' +
@@ -395,6 +394,32 @@ describe('ngRepeat', function() {
395394
});
396395

397396

397+
it('should expose iterator position as $even and $odd when iterating over arrays',
398+
function() {
399+
element = $compile(
400+
'<ul>' +
401+
'<li ng-repeat="item in items">{{item}}:{{$even}}-{{$odd}}|</li>' +
402+
'</ul>')(scope);
403+
scope.items = ['misko', 'shyam', 'doug'];
404+
scope.$digest();
405+
expect(element.text()).
406+
toEqual('misko:true-false|shyam:false-true|doug:true-false|');
407+
408+
scope.items.push('frodo');
409+
scope.$digest();
410+
expect(element.text()).
411+
toBe('misko:true-false|' +
412+
'shyam:false-true|' +
413+
'doug:true-false|' +
414+
'frodo:false-true|');
415+
416+
scope.items.shift();
417+
scope.items.pop();
418+
scope.$digest();
419+
expect(element.text()).toBe('shyam:true-false|doug:false-true|');
420+
});
421+
422+
398423
it('should expose iterator position as $first, $middle and $last when iterating over objects',
399424
function() {
400425
element = $compile(
@@ -420,6 +445,27 @@ describe('ngRepeat', function() {
420445
});
421446

422447

448+
it('should expose iterator position as $even and $odd when iterating over objects',
449+
function() {
450+
element = $compile(
451+
'<ul>' +
452+
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$even}}-{{$odd}}|</li>' +
453+
'</ul>')(scope);
454+
scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'};
455+
scope.$digest();
456+
expect(element.text()).
457+
toBe('doug:d:true-false|' +
458+
'frodo:f:false-true|' +
459+
'misko:m:true-false|' +
460+
'shyam:s:false-true|');
461+
462+
delete scope.items.frodo;
463+
delete scope.items.shyam;
464+
scope.$digest();
465+
expect(element.text()).toBe('doug:d:true-false|misko:m:false-true|');
466+
});
467+
468+
423469
it('should calculate $first, $middle and $last when we filter out properties from an obj', function() {
424470
element = $compile(
425471
'<ul>' +
@@ -435,6 +481,21 @@ describe('ngRepeat', function() {
435481
});
436482

437483

484+
it('should calculate $even and $odd when we filter out properties from an obj', function() {
485+
element = $compile(
486+
'<ul>' +
487+
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$even}}-{{$odd}}|</li>' +
488+
'</ul>')(scope);
489+
scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f', '$toBeFilteredOut': 'xxxx'};
490+
scope.$digest();
491+
expect(element.text()).
492+
toEqual('doug:d:true-false|' +
493+
'frodo:f:false-true|' +
494+
'misko:m:true-false|' +
495+
'shyam:s:false-true|');
496+
});
497+
498+
438499
it('should ignore $ and $$ properties', function() {
439500
element = $compile('<ul><li ng-repeat="i in items">{{i}}|</li></ul>')(scope);
440501
scope.items = ['a', 'b', 'c'];

0 commit comments

Comments
 (0)