0

How to populate select options from a json file using angularjs?

I would like to know how can i populate the following as part of dropdown

todos.products.[DynamicProductName].attributes.Location

DynamicProductName since there are so many different product names under todo.products

http://plnkr.co/edit/LMUqxL4ykOGvTsfFTUoN?p=preview

2 Answers 2

1

ngOptions as well as ngRepeat both support iterating over collections as well as objects, which is important for your data. Those DynamicProductName values are really just keys within the products object.

The basic syntax for using ngOptions in this situation is:

<select ng-model="test" ng-options="key as value for (key , value) in data"></select>

In this case, key would be what would be stored in your model while value would be displayed within the select.

Your value is slightly more nested and thus would be: value.attributes.location.

If you would prefer to have the value (possibly more relevant data), then you could change the code to:

<select 
    data-ng-model="test" 
    data-ng-options="value as value.attributes.location for (key , value) in todos.products"
    >
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your explanation. Really helpful
0
<select data-ng-model="test"
data-ng-options="product.attributes.location for product in todos.products">
</select>

This is pretty longwinded. It may be better to put a products array on the scope and ng-options over that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.