I am trying to do some things in Angular 2 Alpha 28, and am having an issue with dictionaries and NgFor.
I have an interface in TypeScript looking like this:
Here's a variation on some of the above answers that supports multiple transforms (keyval, key, value):
import { Pipe, PipeTransform } from '@angular/core';
type Args = 'keyval'|'key'|'value';
@Pipe({
name: 'mapToIterable',
pure: false
})
export class MapToIterablePipe implements PipeTransform {
transform(obj: {}, arg: Args = 'keyval') {
return arg === 'keyval' ?
Object.keys(obj).map(key => ({key: key, value: obj[key]})) :
arg === 'key' ?
Object.keys(obj) :
arg === 'value' ?
Object.keys(obj).map(key => obj[key]) :
null;
}
}
Usage
map = {
'a': 'aee',
'b': 'bee',
'c': 'see'
}
{{o.key}}: {{o.value}}
a: aee
b: bee
c: see
{{o.key}}: {{o.value}}
a: aee
b: bee
c: see
{{k}}
a
b
c
{{v}}
aee
bee
see