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:
- Created 3 years ago
- Reactions:30
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the only way this might be supported would be via the second proposed option - destructured variable assignment:
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.
This is being tracked in the aggregate issue of #43485 for which we need to create a project proposal.