首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Basic Angular -如何将json http响应加载到另一个json对象中

Basic Angular -如何将json http响应加载到另一个json对象中
EN

Stack Overflow用户
提问于 2016-12-14 09:02:50
回答 1查看 599关注 0票数 1

我正在使用fixer.io和money.js来兑换货币。api用于转换货币,而fixer.io是一个获取最新汇率的money.js。我需要将最新的汇率加载到money.js rates对象中。

因为我使用的是angular,所以money.js的加载方式如下:

代码语言:javascript
复制
var fx = require("money");

为了使转换起作用,我们必须像这样定义fx.basefx.rates

代码语言:javascript
复制
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对象中。

做一些事情的正确方法是什么:

代码语言:javascript
复制
var response = $http.get("http://api.fixer.io/latest");
fx.rates = response;
EN

回答 1

Stack Overflow用户

发布于 2016-12-14 09:40:13

在Angular中使用http promise非常简单。要处理promise,可以使用.then方法。您所需要的只是一个处理数据的回调函数。:

代码语言:javascript
复制
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,如果您需要的话。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41133134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档