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:
interface Dictionary {
[ index: string ]: string
}
In JavaScript this will translate to an object that with data might look like this:
myDict={'key1':'value1','key2':'value2'}
I want to iterate over this and tried this:
<div *ngFor="(#key, #value) of myDict">{{key}}:{{value}}</div>
But to no avail, none of the below worked either:
<div *ngFor="#value of myDict">{{value}}</div>
<div *ngFor="#value of myDict #key=index">{{key}}:{{value}}</div>
In all cases I get errors like "Unexpected token" or "Cannot find 'iterableDiff' pipe supporting object"
What am I missing here? Is this not possible anymore? (The first syntax works in Angular 1.x) or is the syntax different for iterating over an object?
If you have
es6-shim
or yourtsconfig.json
targetes6
, you could use ES6 Map to make it.If someone is wondering how to work with multidimensional object, here is the solution.
lets assume we have following object in service
in component add following function
finally in view do following
Define the
MapValuesPipe
and implement PipeTransform:Add your pipe in your pipes module. This is important if you need to use the same pipe in more than one components:
Then simply use the pipe in your html with
*ngFor
:<tr *ngFor="let attribute of mMap | mapValuesPipe">
Remember, you will need to declare your PipesModule in the component where you want to use the pipe: