Skip to content

Control flow - template used in multiple else clauses #52854

Closed

Description

@kbrilla

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

No response

Description

When the same template is used in multiple*ngIf else clauses then te code is incorectly migrated

<div *ngIf="hasPermission; else noPermission">presentation</div>
<div *ngIf="someOtherPermission; else noPermission">presentation</div>
<ng-template #noPermission>
  <div>Imagine more than just some text</div>
</ng-template>
@if (hasPermission) {
<div>presentation</div>
} @else {

  <div>Imagine more than just some text</div>

}
@if (someOtherPermission) {
<div>presentation</div>
} @else {
#noPermission|
}
<ng-template #noPermission>
  <div>Imagine more than just some text</div>
</ng-template>

Proposed solution

  1. Use <ng-container *ngTemplateOutlet="noPermission"> and leave <ng-template #noPermission>

Alternatives considered

  1. Just duplicate template code and remove template

Not sure which is better so mayby allow for interactive migration and let user decide? Or at least flag for user preference.

Activity

  1. added
    area: migrations
    core: control flow
    on Nov 13, 2023
  2. added this to the needsTriage milestone on Nov 13, 2023
  3. kbrilla commented on Nov 13, 2023

    @kbrilla
    ContributorAuthor

    One more thing to consider, when template is used in both else clause and for ngTemplateOutlet then code inside tempalte is duplicated as well - so mayby here also flag or interactive migration could come in handy

    @if (hasPermission) {
    <div>presentation</div>
    } @else {
    
      <div>Imagine more than just some text</div>
    
    }
    <ng-container *ngTemplateOutlet="noPermission"></ng-container>
    <ng-template #noPermission>
      <div>Imagine more than just some text</div>
    </ng-template>
  4. added a commit that references this issue on Nov 13, 2023
    aa7ccb0
  5. jessicajaniuk commented on Nov 13, 2023

    @jessicajaniuk
    Contributor

    Thanks for reporting this! There's a fix in #52863.

  6. added a commit that references this issue on Nov 13, 2023
    3831942
  7. angular-automatic-lock-bot commented on Dec 14, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Dec 14, 2023
  9. added a commit that references this issue on Jan 23, 2024
    5396b88
  10. added 2 commits that reference this issue on Jan 26, 2024
    0e7b1da
    2be3733
  11. added a commit that references this issue on Jan 26, 2024
    66962df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: migrationscore: control flow

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions