MCPcopy Create free account
hub / github.com/angular/angular / ɵɵconditional

Function ɵɵconditional

packages/core/src/render3/instructions/control_flow.ts:183–239  ·  view source on GitHub ↗
(matchingTemplateIndex: number, contextValue?: T)

Source from the content-addressed store, hash-verified

181 * @codeGenApi
182 */
183export function ɵɵconditional<T>(matchingTemplateIndex: number, contextValue?: T) {
184 performanceMarkFeature('NgControlFlow');
185
186 const hostLView = getLView();
187 const bindingIndex = nextBindingIndex();
188 const prevMatchingTemplateIndex: number =
189 hostLView[bindingIndex] !== NO_CHANGE ? hostLView[bindingIndex] : -1;
190 const prevContainer =
191 prevMatchingTemplateIndex !== -1
192 ? getLContainer(hostLView, HEADER_OFFSET + prevMatchingTemplateIndex)
193 : undefined;
194 const viewInContainerIdx = 0;
195
196 if (bindingUpdated(hostLView, bindingIndex, matchingTemplateIndex)) {
197 const prevConsumer = setActiveConsumer(null);
198 try {
199 // The index of the view to show changed - remove the previously displayed one
200 // (it is a noop if there are no active views in a container).
201 if (prevContainer !== undefined) {
202 removeLViewFromLContainer(prevContainer, viewInContainerIdx);
203 }
204
205 // Index -1 is a special case where none of the conditions evaluates to
206 // a truthy value and as the consequence we've got no view to show.
207 if (matchingTemplateIndex !== -1) {
208 const nextLContainerIndex = HEADER_OFFSET + matchingTemplateIndex;
209 const nextContainer = getLContainer(hostLView, nextLContainerIndex);
210 const templateTNode = getExistingTNode(hostLView[TVIEW], nextLContainerIndex);
211
212 const dehydratedView = findAndReconcileMatchingDehydratedViews(
213 nextContainer,
214 templateTNode,
215 hostLView,
216 );
217 const embeddedLView = createAndRenderEmbeddedLView(hostLView, templateTNode, contextValue, {
218 dehydratedView,
219 });
220
221 addLViewToLContainer(
222 nextContainer,
223 embeddedLView,
224 viewInContainerIdx,
225 shouldAddViewToDom(templateTNode, dehydratedView),
226 );
227 }
228 } finally {
229 setActiveConsumer(prevConsumer);
230 }
231 } else if (prevContainer !== undefined) {
232 // We might keep displaying the same template but the actual value of the expression could have
233 // changed - re-bind in context.
234 const lView = getLViewFromLContainer<T | undefined>(prevContainer, viewInContainerIdx);
235 if (lView !== undefined) {
236 lView[CONTEXT] = contextValue;
237 }
238 }
239}
240

Callers 1

CompClass · 0.90

Calls 13

performanceMarkFeatureFunction · 0.90
getLViewFunction · 0.90
nextBindingIndexFunction · 0.90
bindingUpdatedFunction · 0.90
setActiveConsumerFunction · 0.90
addLViewToLContainerFunction · 0.90
shouldAddViewToDomFunction · 0.90
getLViewFromLContainerFunction · 0.90
getLContainerFunction · 0.85

Tested by

no test coverage detected