2

There is a brandsByLetter class:

brandsByLetter: { [id: string] : AutoServiceBrand[]; } = {};

And I have this construction in angularJS that I need to make in Angular11:

<li *ngFor="let (letter, brands) of brandsByLetter">
    {{letter}}
    {{brands}}
</li>

How do I make "(letter, brands) of" in Angular?

mr_blond
  • 1,586
  • 2
  • 20
  • 52
  • @NadhirFalta It works but when I use type X instead of any tslint writes "Argument type X is not assignable to parameter type ReadonlyMap ". How it can be solved? – mr_blond Feb 23 '21 at 19:14

1 Answers1

2

You can use a pipe for that:

<div *ngFor="let item of brandsByLetter | keyvalue">
  {{item.key}}:{{item.value}}
</div>

add this pipe in your code:

@Pipe({ name: 'keys',  pure: false })
    export class KeysPipe implements PipeTransform {
        transform(value: any, args: any[] = null): any {
            return Object.keys(value)//.map(key => value[key]);
        }
    }

Here is a working example in Stackblitz:
https://stackblitz.com/edit/angular-ahsvig?file=src%2Fapp%2Fapp.component.ts

Nadhir Falta
  • 5,047
  • 2
  • 21
  • 43