aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/widgets.js3
-rw-r--r--test/widgetsSpec.js6
3 files changed, 9 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b56a8bd6..b46b3c2a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@
### Bug Fixes
- Number filter would return incorrect value when fractional part had leading zeros.
- Issue #399: return unsorted array if no predicate
+- Fixed issues with incorrect value of $position in ng:repeat when collection size changes
### Breaking changes
diff --git a/src/widgets.js b/src/widgets.js
index a9d42bdf..392771e3 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -1143,6 +1143,9 @@ angularWidget('@ng:repeat', function(expression, element){
childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key;
lastIterElement = childScope.$element;
+ childScope.$position = index == 0
+ ? 'first'
+ : (index == collectionLength - 1 ? 'last' : 'middle');
childScope.$eval();
} else {
// grow children
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js
index d3b2ec5d..aadc1597 100644
--- a/test/widgetsSpec.js
+++ b/test/widgetsSpec.js
@@ -893,7 +893,11 @@ describe("widget", function(){
it('should expose iterator position as $position when iterating over arrays', function() {
var scope = compile('<ul><li ng:repeat="item in items" ' +
'ng:bind="item + \':\' + $position + \'|\'"></li></ul>');
- scope.items = ['misko', 'shyam', 'doug', 'frodo'];
+ scope.items = ['misko', 'shyam', 'doug'];
+ scope.$eval();
+ expect(element.text()).toEqual('misko:first|shyam:middle|doug:last|');
+
+ scope.items.push('frodo');
scope.$eval();
expect(element.text()).toEqual('misko:first|shyam:middle|doug:middle|frodo:last|');
});