ng-options for select help

17 views
Skip to first unread message

Dave Abbott

unread,
Sep 10, 2015, 3:20:31 PM9/10/15
to AngularJS
I need to populate a <select> with a list of countries.

This is as close as I get.

HTML:
<select id="country_id" type="text" class="form-control" ng-model="formData.countries" ng-options="k as v for (k,v) in  formData.countries track by v"/></select>

So my select initially has all the countries pulled from the database and returned in $scope.formData;

so the raw data is a key value array of data like:

{

..... ohter data

countries:
["Afghanistan",
"Albania",................
"Zimbabwe"]
}

When I select a country it then changes to the key so if i select USA the select now displays 142

Tried every way I can think of and no go.

Any help would be great.

Thanks,

Dave

Dave Abbott

unread,
Sep 10, 2015, 3:35:47 PM9/10/15
to AngularJS
OK I managed I think to get what I want by altering the data array and calling it like:

<select id="country_id" type="text" class="form-control" ng-model="formData.country_id" ng-value="formData.country_id" ng-options="country.id as country.name for country in formData.countries"/>

But when I submit the form its not sending the formData.country_id its sending all the formData.countries array.

Any ideas where I went wrong?

Sander Elias

unread,
Sep 11, 2015, 2:08:24 AM9/11/15
to AngularJS
Hi Dave,

Don't keep your countrylist inside your formData might be an option? Move it to an separate model.

Regards
Sander

Dave

unread,
Sep 11, 2015, 10:32:11 AM9/11/15
to ang...@googlegroups.com
thanks...I noticed grouping them how I had it was the problem. Outside of the formdata now and perfect.

Sent from my rotary phone.
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/Oc4uBl2NwCw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages