我正在使用fixer.io和money.js来兑换货币。api用于转换货币,而fixer.io是一个获取最新汇率的money.js。我需要将最新的汇率加载到money.js rates对象中。
因为我使用的是angular,所以money.js的加载方式如下:
var fx = require("money");为了使转换起作用,我们必须像这样定义fx.base和fx.rates:
fx.base = "USD";
fx.rates = {
"EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
"GBP" : 0.647710, // etc...
"HKD" : 7.781919,
"USD" : 1, // always include the base rate (1:1)
/* etc */
} 但是,对于要从GET请求中填充的fx.rates的硬编码数据,JSON API将返回以下fixer.io:http://api.fixer.io/latest
我完全是angular的新手,所以我不知道如何将json响应加载到另一个json对象中。
做一些事情的正确方法是什么:
var response = $http.get("http://api.fixer.io/latest");
fx.rates = response;发布于 2016-12-14 09:40:13
在Angular中使用http promise非常简单。要处理promise,可以使用.then方法。您所需要的只是一个处理数据的回调函数。:
var response = $http.get("http://api.fixer.io/latest");
//handle promise
response.then(function(response) {
//this is response from the api fixer. The data is the body payload
fx.rates = response.data;
}, function(error) {
//handle error here, if there is any.
});这是working plnkr,如果您需要的话。
https://stackoverflow.com/questions/41133134
复制相似问题