Skip to content

Commit ee7934e

Browse files
committed
fix(ng_repeat): fix ng_repeat not moving views for elements that have not moved
Closes dart-archive#1154
1 parent 1c7c0ba commit ee7934e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/directive/ng_repeat.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class NgRepeat {
8787
Function _generateId = (key, value, index) => value;
8888
Watch _watch;
8989

90-
NgRepeat(this._viewPort, this._boundViewFactory, this._scope,
91-
this._parser, this.formatters);
90+
NgRepeat(this._viewPort, this._boundViewFactory, this._scope, this._parser, this.formatters);
9291

9392
set expression(value) {
9493
assert(value != null);
@@ -220,9 +219,14 @@ class NgRepeat {
220219
var changeFn = changeFunctions[targetIndex];
221220
if (changeFn == null) {
222221
rows[targetIndex] = _rows[targetIndex];
223-
domIndex--;
224222
// The element has not moved but `$last` and `$middle` might still need
225223
// to be updated
224+
var childContext = _updateContext(rows[targetIndex].scope.context, targetIndex, length);
225+
if (domIndex < 0 || leftInDom[domIndex] != targetIndex) {
226+
_viewPort.move(rows[targetIndex].view, moveAfter: previousView);
227+
leftInDom.remove(targetIndex);
228+
}
229+
domIndex--;
226230
_updateContext(rows[targetIndex].scope.context, targetIndex, length);
227231
} else {
228232
changeFn(targetIndex, previousView);

test/directive/ng_repeat_spec.dart

+8
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ main() {
420420
lis = element.querySelectorAll('li');
421421
});
422422

423+
it(r'should correctly update rows orders - gh1154', () {
424+
scope.context['items'] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
425+
scope.apply();
426+
expect(element).toHaveText('0123456789');
427+
scope.context['items'] = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0];
428+
scope.apply();
429+
expect(element).toHaveText('0123456789');
430+
});
423431

424432
it(r'should preserve the order of elements', () {
425433
scope.context['items'] = [a, c, d];

0 commit comments

Comments
 (0)