question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a shorter alias for ng-container

See original GitHub issue

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

core

Description

ng-container tends to be used a lot. Especially if you don’t want erroneous elements appearing in your html that are there only because of some conditional checks. This becomes verbose. Perhaps we can have a short alias for ng-container?

We already have short forms for the template with things with ngFor / ngForOf etc.

E.g. This is a an OTT example to highlight some common use cases

<ng-container *ngIf="...">
  <ng-container *ngFor="...">
    <ng-container [ngSwitch]="...">
      <ng-container *ngSwitchCase="'...'">
        ...
      </ng-container>
      <ng-container *ngSwitchCase="'...'">
        ...
      </ng-container>
      <ng-container *ngSwitchCase="'...'">
        ...
      </ng-container>
    </ng-container>
  </ng-container>
</ng-container>

Proposed solution

I would love to see a short alias for the container like

<ng *ngIf="...">
  <ng *ngFor="...">
    <ng [ngSwitch]="...">
      <ng *ngSwitchCase="'...'">
        ...
      </ng>
      <ng *ngSwitchCase="'...'">
        ...
      </ng>
      <ng *ngSwitchCase="'...'">
        ...
      </ng>
    </ng>
  </ng>
</ng>

Alternatives considered

Containers could be marked implicitly using an unnamed asterisk attribute

<ng-for *="value of values">
  <ng-if *="value">
    Show something
  </ng-if>
</ng-for>

OR

<ngFor *="value of values">
  <ngIf *="value">
    Show something
  </ngIf>
</ngFor>

Or perhaps a different shorter element name other than <ng …?

EDIT 1: add some clarification EDIT 2: removed bad alternative, add some more context EDIT 3: update alternative with suggestion from @stupidawesome below EDIT 4: added another alternative that doesn’t dash-case the selector

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:39
  • Comments:33 (16 by maintainers)

github_iconTop GitHub Comments

8reactions
Airbladercommented, Jul 31, 2021

I know that the voting process doesn’t actually include “thumb downs”, but I’ll just explain my vote:

  • I don’t think the two shown snippets differ fundamentally in complexity or readability. If that is a concern, component separation would be the way to go.
  • Typing a couple extra characters is really not an issue in good IDEs anymore. Making everything shorter always comes at the cost of potentially making it less understandable for people not familiar with it.
  • Obviously just changing it wouldn’t be possible due to being a breaking change, and adding configuration for such a (IMO) non-issue feels too much like bloating. We might as well make it configurable to write @C instead of @Component etc.
  • Making such fundamental things configurable breaks familiarity for people coming into a project.
  • With the team’s process of trying to focus on the most important issues and closing others, I feel this just wouldn’t make for well-invested time (again, IMO).
6reactions
kbrillacommented, Jul 31, 2021

I would go further than shorthand and propose an empty tag mayby? Why? - Because it’s even shorter and <ng-container> is an element does not end up in DOM anyway

< *ngIf="...">
  < *ngFor="...">
    < [ngSwitch]="...">
      < *ngSwitchCase="'...'">
        ...
      </>
      < *ngSwitchCase="'...'">
        ...
      </>
      <*ngSwitchCase="'...'">
        ...
      </>
    </>
  </>
</>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular template: use "as"-generated alias in the same ...
I have an instruction like this: <ng-container *ngIf="(someService.modules$ | async | filterByPurchased:true) as purchasedModules"> some ...
Read more >
Generating Angular Container Components, the Fast and ...
g is the short alias for generate . c is the short alias for component . next we specify the path as to...
Read more >
NgForOf - Angular
When using the shorthand syntax, Angular allows only one structural directive on an element. If you want to iterate conditionally, for example, put...
Read more >
How to set an alias in Windows Command Line? - Super User
and if you type prt This is short. then the output will be: ... Basically I created a file named fav.bat and put...
Read more >
How to avoid multiple async pipes in Angular | by Yury Katkov
The code becomes smaller and much easier to read. ... We wrap the html tags into a div , span or ng-container ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found