Last active
May 2, 2024 02:29
-
-
Save nikoloza/9018ee9c3fbad07292cc to your computer and use it in GitHub Desktop.
[Depricated - it's no longer free] Universal currency exchanger in Angular.js using freecurrencyconverterapi.com public API (ES5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.service('CurrencyExchange', function($http, $q) { | |
// using public API for currency exchange rates | |
return function (from, rates) { | |
var deferred = $q.defer(); | |
var base = from.toUpperCase(); | |
var currencies = []; | |
if (typeof rates === 'string') { | |
currencies = base + '_' + rates.toUpperCase(); | |
} | |
else { | |
rates.forEach(function(value){ | |
currencies.push(base + '_' + value.toUpperCase()); | |
}); | |
currencies = currencies.join(','); | |
} | |
$http.jsonp('http://www.freecurrencyconverterapi.com/api/v3/convert?q=' + currencies + '&compact=ultra&callback=JSON_CALLBACK') | |
.success(function(data) { | |
var prettyData = { | |
base: base, | |
rates: [] | |
}; | |
Object.keys(data).forEach(function(key){ | |
prettyData.rates.push({ | |
key: key.split('_')[1], | |
value: data[key] | |
}); | |
}); | |
deferred.resolve(prettyData); | |
}); | |
return deferred.promise; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Controller should look like this:
and the view: