Skip to content

fix(compiler): support foreign components inside control flow blocks#69674

Open
leonsenft wants to merge 3 commits into
angular:mainfrom
leonsenft:foreign-component-conditional
Open

fix(compiler): support foreign components inside control flow blocks#69674
leonsenft wants to merge 3 commits into
angular:mainfrom
leonsenft:foreign-component-conditional

Conversation

@leonsenft

@leonsenft leonsenft commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Prepend generated view scope variables to view.create in addition to view.update so that expressions evaluated during creation (such as foreign component property bindings) can resolve context variables from parent views when nested inside control flow blocks (@if, @switch, @for). This is necessary to support binding properties to foreign components inside control flow blocks.

@leonsenft leonsenft added area: compiler Issues related to `ngc`, Angular's template compiler target: minor This PR is targeted for the next minor release labels Jul 7, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jul 7, 2026
@angular-robot angular-robot Bot added the area: performance Issues related to performance label Jul 7, 2026
@leonsenft leonsenft force-pushed the foreign-component-conditional branch from 3a0f32b to 6e3a9ca Compare July 7, 2026 21:10
@leonsenft leonsenft requested a review from mattrbeck July 7, 2026 21:53
@leonsenft leonsenft marked this pull request as ready for review July 7, 2026 21:53
}
}

view.create.prepend(generateVariablesInScopeForView(view, scope, false));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially problematic for variables which aren't available in the creation pass, e.g. @if (c) { <input #i /> <FancyButton [x]="i" /> }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great catch, and one that isn't trivial to solve. The issue is that these references truly aren't available until the update phase, so we can't simply expose them to the create phase like we did for the component context.

As a workaround, you could use viewQuery() signal to pass reactive references, although this similarly breaks if you use viewQuery.required().

We could implement diagnostics to prohibit passing references in this manner. Alternatively, we could reevaluate the timing of foreign component creation and move it to the update phase. That would be a pretty significant refactor though, so are you okay if we submit this as-is for now and address that in a follow up PR?

leonsenft added 2 commits July 8, 2026 22:57
…n component

When a control flow block (`@if`, `@switch`, `@for`) contains a single
root element that is a foreign component, do not treat its name as a tag
name for the template container (`conditionalCreate`, `repeaterCreate`).
Prepend generated view scope variables to `view.create` in addition to
`view.update` so that expressions evaluated during creation (such as
foreign component property bindings) can resolve context variables from
parent views when nested inside control flow blocks (`@if`, `@switch`,
`@for`). This is necessary to support binding properties to foreign
components inside control flow blocks.
reflect this broader behavior.)
@leonsenft leonsenft force-pushed the foreign-component-conditional branch from 6e3a9ca to 962abec Compare July 8, 2026 22:57
@angular-robot angular-robot Bot requested a review from mattrbeck July 8, 2026 22:57
@leonsenft leonsenft force-pushed the foreign-component-conditional branch from 962abec to 66d91f2 Compare July 8, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: compiler Issues related to `ngc`, Angular's template compiler area: performance Issues related to performance target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants