Reputation: 3391
I'm testing Angular's keyvalue
pipe with simple code:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<div *ngFor="let prop of testObj | keyvalue">
<div>key: {{prop.key}}</div>
<div>value: {{prop.value}}<div>
</div> `
})
export class AppComponent {
testObj = { id: 1, name: "Abdul Rafay" }
}
but it's giving me this error:
Template parse errors: The pipe 'keyvalue' could not be found ("]prop of testObj | keyvalue"> key: {{prop.key}} value: {{prop.value}}"): ng:///AppModule/AppComponent.html@0:17 Evaluating src/main.ts Booting application
am i missing anything? Here's my Stackblitz
Upvotes: 12
Views: 17001
Reputation: 303
Angular 16: In my case I had to add import { KeyValuePipe } from '@angular/common';
Upvotes: 1
Reputation: 27371
The KeyValue Pipe is available in angular 6.1 to Update your dependencies it will work
If you are using angular 6 you can try this
HTML
<div *ngFor="let prop of key">
<div>key: {{prop}}</div>
<div>value: {{testObj[prop]}}<div>
</div>
TS
testObj = { id: 1, name: "Abdul Rafay" }
get key(){
return Object.keys(this.testObj);
}
Example: https://stackblitz.com/edit/angular-6-template-x9hady
Upvotes: 6