fix(ivy): support inserting a viewRef that is already present (#34052)

When inserting a `viewRef` it is possible to not provide
an `index`, which is regarded as appending to the end of
the container.

If the `viewRef` already exists in the container, then
this results in a move. But there was a fault in the logic
that computed where to insert the `viewRef` that did not
account for the fact that the `viewRef` was already in
the container, so the insertion `index` was outside the
bounds of the array.

Fixes #33924

PR Close #34052
This commit is contained in:
Pete Bacon Darwin
2019-11-26 12:09:28 +00:00
committed by Matias Niemelä
parent b659aa32c9
commit 978b500961
2 changed files with 20 additions and 2 deletions

View File

@ -245,14 +245,16 @@ export function createContainerRef(
}
this.allocateContainerIfNeeded();
const lView = (viewRef as ViewRef<any>)._lView !;
const adjustedIdx = this._adjustIndex(index);
if (viewAttachedToContainer(lView)) {
// If view is already attached, fall back to move() so we clean up
// references appropriately.
return this.move(viewRef, adjustedIdx);
// Note that we "shift" -1 because the move will involve inserting
// one view but also removing one view.
return this.move(viewRef, this._adjustIndex(index, -1));
}
const adjustedIdx = this._adjustIndex(index);
insertView(lView, this._lContainer, adjustedIdx);
const beforeNode = getBeforeNodeForView(adjustedIdx, this._lContainer);