Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用angular2读取JSON_Json_Angular - Fatal编程技术网

使用angular2读取JSON

使用angular2读取JSON,json,angular,Json,Angular,我对json\object有问题,我试图从中提取数据,但失败了 我有一个API,我从中提取数据: 我已将其放置在变量results上, 如果我想获得对象参数date、base、rate,如下所示: calc(details) { let results = [this.networkServices.getCurrency(details)]; // object is here "getCurrency deal with the GET request. alert(res

我对json\object有问题,我试图从中提取数据,但失败了

我有一个API,我从中提取数据:

我已将其放置在变量
results
上, 如果我想获得对象参数date、base、rate,如下所示:

calc(details) {
    let results = [this.networkServices.getCurrency(details)]; // object is here "getCurrency deal with the GET request.
    alert(results.base);
}
我得到了错误代码:

[02:58:36]  transpile update started ...
[02:58:38]  typescript: D:/ionic/firstApp/src/pages/currency/currency.ts, line: 19 
            Property 'base' does not exist on type 'Promise<any>[]'.

      L18:  let results = [this.networkServices.getCurrency(details)];
      L19:  alert(results.base);

[02:58:38]  transpile update failed 

服务请求是异步的,因此请求的结果是解析为对象的承诺,而不是对象本身。试着这样做:

this.networkServices.getCurrency(details).then(result => alert(result))

尝试更新您的
getCurrency()
,删除
then()
,以返回承诺:

那么@pe8ter的解决方案应该有效:

this.networkServices.getCurrency(详细信息)。然后(结果=>alert(结果))

请使用网络服务中的getCurrency()方法进行更新这与Angular有什么关系?这与JSON有什么关系?
this.networkServices.getCurrency(details).then(result => alert(result))
getCurrency(obj){
    console.log("function fired!")
    let url = `http://api.fixer.io/latest?base=${obj.selectedCurrency}`;
    return this.http.get(url).toPromise();
  }