I'm still trying to find my way with AngularJS. I have a JavaScript code that uses URL to return JSON data as an array. I need help with populating the same data in select using ng-options.
data to populate on the select
Asked
Active
Viewed 556 times
0

eldix_
- 127
- 4
- 20
-
6Can you post what you have tried so far? – user2085143 Nov 23 '16 at 15:16
-
http://stackoverflow.com/questions/13047923/working-with-select-using-angulars-ng-options might prove useful – edo.n Nov 23 '16 at 15:23
-
Thanks man, the link helped I now see the light. – eldix_ Nov 23 '16 at 15:49
1 Answers
0
This isn't how you ask for help, but nevermind. Given a JSON object like this
var JsonArray = [{
id: 1,
name: 'Jane',
address: 'Jane\'s house'
}, {
id: 2,
name: 'Jill',
address: 'Jill\'s house'
}, {
id: 3,
name: 'John',
address: 'John\'s house'
}, {
id: 4,
name: 'Jack',
address: 'Jack\'s house'
}];
When you want to use ng-select with ng-options, you need to specify 3 required fields :
your table
the name that every object will take (like a for ... each loop)
The property you want to see in your options
You also can specify a track by
property to give your options a given value.
<select ng-model="mySelect" ng-options="object.name for object in JsonArray track by object.id"></select>
Now, use the last piece of code, and inspect it in a brwoser. You will understand everything.
For reference :
<select ng-model="mySelect" ng-options="[object].[chosenProperty] for [object] in [yourArray] track by [object].[property]"></select>
-
thanks. I copied the results and created a JSON file like you've done. It's working fine, what if I didn't have a file or don't have to create that file? How can I do it without creating a file but to reference the output as displayed on the console. – eldix_ Nov 23 '16 at 15:48
-
Sorry I don't understand your question. What do you exactly want to do ? Because I didn't create any file ... – Nov 24 '16 at 08:22
-
I wanted to reuse the output of the URL call from the javaScript which display that JSON data you've created at the top. I got it to work, it was just a matter of assigning ng-model withiin the – eldix_ Nov 24 '16 at 12:20