Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular 17 New Control Flow | Allow more than one node at root @if, @else, @for, etc #53677

Open
WarGreyGon opened this issue Dec 21, 2023 · 4 comments
Labels
area: core Issues related to the framework runtime core: content projection feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Milestone

Comments

@WarGreyGon
Copy link

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

Allow multiple child nodes inside new control flow like bellow

<app-box>
  @if (selected === 'fish'){
    <span header> Fish </span>
    <span main> 🐟 🐡 🐙 🐠  </span>
  } @else {
    <span header> Mammals </span>
    <span main> 🦍 🐅 🦬 🐫  </span>
  }
</app-box>

without throwing bellow error

Node matches the "[header]" slot of the "BoxComponent" component, but will not be projected into the specific slot because the surrounding @if has more than one node at its root. To project the node in the right slot, you can:

  1. Wrap the content of the @if block in an that matches the "[header]" selector.
  2. Split the content of the @if block across multiple @if blocks such that each one only has a single projectable node at its root.
  3. Remove all content from the @if block, except for the node being projected.

This check can be disabled using the extendedDiagnostics.checks.controlFlowPreventingContentProjection = "suppress" compiler option.

Proposed solution

Make bellow snippet work

<app-box>
  @if (selected === 'fish'){
    <span header> Fish </span>
    <span main> 🐟 🐡 🐙 🐠  </span>
  } @else {
    <span header> Mammals </span>
    <span main> 🦍 🐅 🦬 🐫  </span>
  }
</app-box>

Alternatives considered

Since I am using projections I have to use multiple @if

<app-box>
@if (selected === 'fish'){
  <span header> Fish </span>
}    
@if (selected === 'mammals'){
  <span header> Mammals </span>
}
@if (selected === 'fish'){
  <span main> 🐟 🐡 🐙 🐠  </span>
}
@if (selected === 'mammals'){
  <span main> 🦍 🐅 🦬 🐫  </span>
}
</app-box>

but this approach is a pain in the *ss, more verbose and less readable

@JeanMeche
Copy link
Member

Hi, this has been discussed in #53477. This is known limitation of content projection.

@JoostK
Copy link
Member

JoostK commented Dec 21, 2023

This has been discussed when designing the content projection behavior with the new control flow, and there isn't a feasible way to support these cases. It would require splitting the template into multiple embedded views, but this introduces problems where template references cannot be accessed across those distinct embedded views.

It becomes more problematic with @for, where insertions and deletions of rows would have to be performed to multiple DOM parent nodes.

@JoostK JoostK added area: core Issues related to the framework runtime core: content projection core: control flow Issues related to the built-in control flow (@if, @for, @switch) labels Dec 21, 2023
@ngbot ngbot bot modified the milestone: needsTriage Dec 21, 2023
@pkozlowski-opensource pkozlowski-opensource added the feature Issue that requests a new feature label Jan 3, 2024
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jan 3, 2024
@pkozlowski-opensource pkozlowski-opensource removed the core: control flow Issues related to the built-in control flow (@if, @for, @switch) label Jan 3, 2024
@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jan 5, 2024
Copy link
Contributor

angular-robot bot commented Jan 5, 2024

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

Copy link
Contributor

angular-robot bot commented Feb 14, 2024

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime core: content projection feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

4 participants