Skip to content

Commit 9ec45ad

Browse files
mheveryIgorMinar
authored andcommitted
fix:ng:repeater - fix $position when collection size changes
1 parent 8e880fc commit 9ec45ad

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Bug Fixes
1111
- Number filter would return incorrect value when fractional part had leading zeros.
1212
- Issue #399: return unsorted array if no predicate
13+
- Fixed issues with incorrect value of $position in ng:repeat when collection size changes
1314

1415

1516
### Breaking changes

src/widgets.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,9 @@ angularWidget('@ng:repeat', function(expression, element){
11431143
childScope[valueIdent] = collection[key];
11441144
if (keyIdent) childScope[keyIdent] = key;
11451145
lastIterElement = childScope.$element;
1146+
childScope.$position = index == 0
1147+
? 'first'
1148+
: (index == collectionLength - 1 ? 'last' : 'middle');
11461149
childScope.$eval();
11471150
} else {
11481151
// grow children

test/widgetsSpec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,11 @@ describe("widget", function(){
893893
it('should expose iterator position as $position when iterating over arrays', function() {
894894
var scope = compile('<ul><li ng:repeat="item in items" ' +
895895
'ng:bind="item + \':\' + $position + \'|\'"></li></ul>');
896-
scope.items = ['misko', 'shyam', 'doug', 'frodo'];
896+
scope.items = ['misko', 'shyam', 'doug'];
897+
scope.$eval();
898+
expect(element.text()).toEqual('misko:first|shyam:middle|doug:last|');
899+
900+
scope.items.push('frodo');
897901
scope.$eval();
898902
expect(element.text()).toEqual('misko:first|shyam:middle|doug:middle|frodo:last|');
899903
});

0 commit comments

Comments
 (0)