angularjs - Angular 2: Create drop-down list with array of dictionaries -
this question has answer here:
i'm using options tag in angular 2 populate dropdown list. dropdown list populating correctly datetest1.json
, however, when iterating thru values of given key in .json file, it's having trouble populating drop down list. what's correct way in angular 2 populating dropdown list datetest.json
example below?
*.component.html
when retrieving datetest.json: exception: typeerror: cannot read property 'month' of undefined in [dateattributeslist.month in *component@63:22]
<option *ngfor="#m of dateattributeslist.month" [value]="m">{{m}}</option>
datetest.json:
[ {"month": ["dec", "jan", ...]}, {"day" : ["1","2"...]} ]
*.component.html when retrieving datetest1.json: correctly populating
<option *ngfor="#m of dateattributeslist" [value]="m">{{m}}</option>
datetest1.json:
[ {"month": "dec"}, {"month": "jan"}, {"month": "feb"}, ... ]
use safe navigation operator if value bind passed input or fetched async call:
<option *ngfor="#m of dateattributeslist?.month" [value]="m.month">{{m}}</option>
[value]
supports string values. if have object values use [ngvalue]
<option *ngfor="#m of dateattributeslist" [ngvalue]="m.month">{{m}}</option>
[ngvalue]
works [ngmodel]
Comments
Post a Comment