diff --git a/lib/directive/ng_repeat.dart b/lib/directive/ng_repeat.dart index 194316c2f..731295c93 100644 --- a/lib/directive/ng_repeat.dart +++ b/lib/directive/ng_repeat.dart @@ -87,8 +87,7 @@ class NgRepeat { Function _generateId = (key, value, index) => value; Watch _watch; - NgRepeat(this._viewPort, this._boundViewFactory, this._scope, - this._parser, this.formatters); + NgRepeat(this._viewPort, this._boundViewFactory, this._scope, this._parser, this.formatters); set expression(value) { assert(value != null); @@ -220,9 +219,14 @@ class NgRepeat { var changeFn = changeFunctions[targetIndex]; if (changeFn == null) { rows[targetIndex] = _rows[targetIndex]; - domIndex--; // The element has not moved but `$last` and `$middle` might still need // to be updated + var childContext = _updateContext(rows[targetIndex].scope.context, targetIndex, length); + if (domIndex < 0 || leftInDom[domIndex] != targetIndex) { + _viewPort.move(rows[targetIndex].view, moveAfter: previousView); + leftInDom.remove(targetIndex); + } + domIndex--; _updateContext(rows[targetIndex].scope.context, targetIndex, length); } else { changeFn(targetIndex, previousView); diff --git a/test/directive/ng_repeat_spec.dart b/test/directive/ng_repeat_spec.dart index 6f2594867..b5613efa9 100644 --- a/test/directive/ng_repeat_spec.dart +++ b/test/directive/ng_repeat_spec.dart @@ -420,6 +420,14 @@ main() { lis = element.querySelectorAll('li'); }); + it(r'should correctly update rows orders - gh1154', () { + scope.context['items'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + scope.apply(); + expect(element).toHaveText('0123456789'); + scope.context['items'] = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0]; + scope.apply(); + expect(element).toHaveText('0123456789'); + }); it(r'should preserve the order of elements', () { scope.context['items'] = [a, c, d];