diff --git a/packages/core/src/render3/instructions/select.ts b/packages/core/src/render3/instructions/select.ts index e24f7c77bb..e10b782fad 100644 --- a/packages/core/src/render3/instructions/select.ts +++ b/packages/core/src/render3/instructions/select.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {assertGreaterThan, assertLessThan} from '../../util/assert'; +import {assertDataInRange, assertGreaterThan} from '../../util/assert'; import {executePreOrderHooks} from '../hooks'; import {HEADER_OFFSET, LView, TVIEW} from '../interfaces/view'; import {getCheckNoChangesMode, getLView, setSelectedIndex} from '../state'; @@ -33,18 +33,15 @@ import {getCheckNoChangesMode, getLView, setSelectedIndex} from '../state'; * @codeGenApi */ export function ɵɵselect(index: number): void { - ngDevMode && assertGreaterThan(index, -1, 'Invalid index'); - ngDevMode && - assertLessThan( - index, getLView().length - HEADER_OFFSET, 'Should be within range for the view data'); - const lView = getLView(); - selectInternal(lView, index); + selectInternal(getLView(), index, getCheckNoChangesMode()); } +export function selectInternal(lView: LView, index: number, checkNoChangesMode: boolean) { + ngDevMode && assertGreaterThan(index, -1, 'Invalid index'); + ngDevMode && assertDataInRange(lView, index + HEADER_OFFSET); -export function selectInternal(lView: LView, index: number) { // Flush the initial hooks for elements in the view that have been added up to this point. - executePreOrderHooks(lView, lView[TVIEW], getCheckNoChangesMode(), index); + executePreOrderHooks(lView, lView[TVIEW], checkNoChangesMode, index); // We must set the selected index *after* running the hooks, because hooks may have side-effects // that cause other template functions to run, thus updating the selected index, which is global diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index 5fa4f23819..7ba9f5cabf 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -452,10 +452,10 @@ function executeTemplate( const prevSelectedIndex = getSelectedIndex(); try { setActiveHostElement(null); - if (rf & RenderFlags.Update) { + if (rf & RenderFlags.Update && lView.length > HEADER_OFFSET) { // When we're updating, have an inherent ɵɵselect(0) so we don't have to generate that // instruction for most update blocks - selectInternal(lView, 0); + selectInternal(lView, 0, getCheckNoChangesMode()); } templateFn(rf, context); } finally {