In our angular application, from controller I have returned a dictionary. How can I display the same in html template using *ngFor?
Please anyone help to achieve the same
You can use the built-in keyvalue pipe like this:
<span *ngFor="let item of content | keyvalue">
Key: {{item.key}}, Value: {{item.value}}
</span>
You can create a pipe that maps the object (dictionary) to an array
containing of type : { key: string,value: any }
Somthing like this:
@Pipe({name: 'mapToArray'})
export class MapToArray implements PipeTransform {
transform(value, args:string[]) : any {
let arr = [];
for (let key in value) {
arr.push({key: key, value: value[key]});
}
return arr;
}
}
and use it like that in the html file:
<span *ngFor="let item of content | mapToArray">
Key: {{item.key}}, Value: {{item.value}}
</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With