Kyle G. Kyle G. - 7 months ago 55
AngularJS Question

How do I correctly bind 'select' option with an API call?

I'm trying to write a simple AngularJS program which makes REST calls to an API that gives data on exchange rates. the app deals with exchange rates, and i'm doing this mainly for educational purposes.
I'm have an issue while attempting to two-way bind data between a select element in the HTML:

<select id="currencySelect"
ng-options="currency for (currency, rate) in exRates.rates">

And my controller:

(function () {
"use strict";
.controller('getRates', function($scope, $http){
$scope.$watch('search', function() {
$ = "USD";

function fetch(){
$http.get("" + $ )
$scope.exRates =;

'search' is bound to the HTML element via ng-model, and works perfectly for the default value set at "USD". However, when I use the select element to select another type of currency, the data bound to 'search' is the rate in the (currency, rate) -- key, value pair.

I want it to take the currency(key) which is being displayed in the select element and bind that to 'search' instead of the rate(value).

For example: If I choose "EUR" in the select element, 'search' is bound to 0.90318 instead of "EUR". The GET request then tries:

which does not work.

It may help to look at a sample JSON excerpt:


{ "base":"USD",
"rates": {

Example of the user interface


You were close, just need to adjust your ngOptions syntax slightly:

ng-options="currency as currency for (currency, rate) in exRates.rates"

Easy to remember with value as text for item/obj in collection