Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* | `$first` | {@type boolean} | true if the repeated element is first in the iterator. |
* | `$middle` | {@type boolean} | true if the repeated element is between the first and last in the iterator. |
* | `$last` | {@type boolean} | true if the repeated element is last in the iterator. |
* | `$even` | {@type boolean} | true if the iterator position `$index` is even (otherwise false). |
* | `$odd` | {@type boolean} | true if the iterator position `$index` is odd (otherwise false). |
*
* Additionally, you can also provide animations via the ngAnimate attribute to animate the **enter**,
* **leave** and **move** effects.
Expand Down Expand Up @@ -345,6 +347,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
childScope.$first = (index === 0);
childScope.$last = (index === (arrayLength - 1));
childScope.$middle = !(childScope.$first || childScope.$last);
childScope.$odd = !(childScope.$even = index%2==0);

if (!block.startNode) {
linker(childScope, function(clone) {
Expand Down
71 changes: 70 additions & 1 deletion test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ describe('ngRepeat', function() {
expect(element.text()).toEqual('misko:0|shyam:1|frodo:2|');
});


it('should expose iterator offset as $index when iterating over objects', function() {
element = $compile(
'<ul>' +
Expand Down Expand Up @@ -386,6 +385,36 @@ describe('ngRepeat', function() {
});


it('should expose iterator position as $even and $odd when iterating over arrays',
function() {
element = $compile(
'<ul>' +
'<li ng-repeat="item in items">{{item}}:{{$even}}-{{$odd}}|</li>' +
'</ul>')(scope);
scope.items = ['misko', 'shyam', 'doug'];
scope.$digest();
expect(element.text()).
toEqual('misko:true-false|shyam:false-true|doug:true-false|');

scope.items.push('frodo');
scope.$digest();
expect(element.text()).
toEqual('misko:true-false|' +
'shyam:false-true|' +
'doug:true-false|' +
'frodo:false-true|');

scope.items.pop();
scope.items.pop();
scope.$digest();
expect(element.text()).toEqual('misko:true-false|shyam:false-true|');

scope.items.pop();
scope.$digest();
expect(element.text()).toEqual('misko:true-false|');
});


it('should expose iterator position as $first, $middle and $last when iterating over objects',
function() {
element = $compile(
Expand All @@ -411,6 +440,31 @@ describe('ngRepeat', function() {
});


it('should expose iterator position as $even and $odd when iterating over objects',
function() {
element = $compile(
'<ul>' +
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$even}}-{{$odd}}|</li>' +
'</ul>')(scope);
scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'};
scope.$digest();
expect(element.text()).
toEqual('doug:d:true-false|' +
'frodo:f:false-true|' +
'misko:m:true-false|' +
'shyam:s:false-true|');

delete scope.items.doug;
delete scope.items.frodo;
scope.$digest();
expect(element.text()).toEqual('misko:m:true-false|shyam:s:false-true|');

delete scope.items.shyam;
scope.$digest();
expect(element.text()).toEqual('misko:m:true-false|');
});


it('should calculate $first, $middle and $last when we filter out properties from an obj', function() {
element = $compile(
'<ul>' +
Expand All @@ -426,6 +480,21 @@ describe('ngRepeat', function() {
});


it('should calculate $even and $odd when we filter out properties from an obj', function() {
element = $compile(
'<ul>' +
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$even}}-{{$odd}}|</li>' +
'</ul>')(scope);
scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f', '$toBeFilteredOut': 'xxxx'};
scope.$digest();
expect(element.text()).
toEqual('doug:d:true-false|' +
'frodo:f:false-true|' +
'misko:m:true-false|' +
'shyam:s:false-true|');
});


it('should ignore $ and $$ properties', function() {
element = $compile('<ul><li ng-repeat="i in items">{{i}}|</li></ul>')(scope);
scope.items = ['a', 'b', 'c'];
Expand Down