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.

feat(compiler): Support destructured variable assignment in structural directives

See original GitHub issue

🚀 feature request

Relevant Package

This feature request is for @angular/common

Description

Using the keyvalue pipe on a Map loses all naming and makes the template difficult to read

Example

@Component{...}
export class MyComponent {
    countryPoiMap = Map<Country, Poi>();
}
<div *ngFor="let countryPoi of countryPoiMap | keyvalue">
   <!-- key is a country -->
   <div>{{ countryPoi.key.name }}</div>
   <!-- value is a poi -->
   <div>{{ countryPoi.value.name }}</div>
</div>

Describe the solution you’d like

It would be great to be able to alias the result of a keyvalue pipe

Example

<div *ngFor="let countryPoi of countryPoiMap | keyvalue:country:poi">
   <div>{{ country.name }}</div>
   <div>{{ poi.name }}</div>
</div>

Other syntax

<div *ngFor="let { key as country, value as poi } of countryPoiMap | keyvalue">
   <div>{{ country.name }}</div>
   <div>{{ poi.name }}</div>
</div>

Other syntax

<div *ngFor="(let countryPoi of countryPoiMap | keyvalue) as [country, poi]">
   <div>{{ country.name }}</div>
   <div>{{ poi.name }}</div>
</div>

Describe alternatives you’ve considered

Implementing an *ngLet directive could alleviate this issue (#15280)

Example

<div *ngFor="let countryPoi of countryPoiMap | keyvalue"
    *ngLet="countryPoi.key as country"
    *ngLet="countryPoi.value as poi"
>
   <div>{{ country.name }}</div>
   <div>{{ poi.name }}</div>
</div>

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:30
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
petebacondarwincommented, Sep 17, 2021

I think the only way this might be supported would be via the second proposed option - destructured variable assignment:

<div *ngFor="let { key as country, value as poi } of countryPoiMap | keyvalue">
   <div>{{ country.name }}</div>
   <div>{{ poi.name }}</div>
</div>

I am going to retitle this issue, with this in mind, since implementing that feature would actually be more general than just the keyvalue pipe, and is more relevant to any structural directive that assigns a value to local context.

0reactions
petebacondarwincommented, Oct 15, 2021

This is being tracked in the aggregate issue of #43485 for which we need to create a project proposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Structural directives - Angular
This guide is about structural directives and provides conceptual information on how such directives work, how Angular interprets their shorthand syntax, ...
Read more >
CompCert: the Double-Edged Sword of Verification - Cornell CS
"Formal verification of a realistic compiler" presents CompCert as the first ... This pass will turn the variable assignment into a nop ....
Read more >
grunt-ts | Yarn - Package Manager
Grunt-ts is an npm package that handles TypeScript compilation work in GruntJS build scripts. This project, much like Grunt itself, is now in...
Read more >
Structure of CompCert - AbsInt
Structure of CompCert, the first formally verified optimizing C compiler in the ... language featuring both structured ( if / else , loops)...
Read more >
Understanding Angular Structural Directives - Netanel Basal
*carousel is the directive name. The * signals that we are dealing with a structural directive. · The let keyword declares a template...
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