简体   繁体   English

了解如何调用REST Api并在Angular 2中显示响应中的数据

[英]Understanding how to call a REST Api and displaying the data from the response in Angular 2

I'm new in Angular 2 and Typescript so please excuse me for this question but I can't understand how to consume the data after successfully calling a REST Api. 我是Angular 2和Typescript的新手,所以请原谅我这个问题,但是在成功调用REST Api之后我无法理解如何使用数据。 I made a plunker for my example so it will be easier to explain what I'm tring to do. 我为我的例子做了一个plunker ,所以更容易解释我要做的事情。

Please ignore unused imports when viewing the example. 查看示例时请忽略未使用的导入。

Calling the function getWeather works fine. 调用函数getWeather工作正常。

getWeather(query) {
        const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98';
        return this.http
            .get(endpoint)//, {search: searchParams})
            .map(res => res.json().main)
            .subscribe(res => console.log('weather json response = ' + JSON.stringify(res))
            );
    }

But how to store the data? 但是如何存储数据呢? I mean do I have to make an object similar like the json response and use if for displaying the data and how? 我的意思是我必须创建一个类似json响应的对象,并使用if如何显示数据以及如何?

Edit: Here is a working example with my code. 编辑: 是我的代码的一个工作示例。

When reverting the data, you simply need to set them as property of the component using the this keyword. 还原数据时,只需使用this关键字将它们设置为组件的属性即可。

In the case of HTTP request, the data are there when the first callback registered using the subscribe method is called. 在HTTP请求的情况下,当调用使用subscribe方法注册的第一个回调时,数据就在那里。

Using an arrow function to define this callback allows to use the component instance through the this keyword (contextual this in this case). 使用箭头函数定义此回调允许通过this关键字使用组件实例(在本例中为contextual)。

getWeather(query) {
  const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98';
  return this.http
      .get(endpoint)//, {search: searchParams})
      .map(res => res.json().main)
      .subscribe(res => {
        this.weather = data;
       });
}

Then the template can reference such properties to display data with ngFor, {{…}} or interpolation. 然后模板可以引用这些属性来显示带有ngFor,{{...}}或插值的数据。 Be careful to handle the asynchronous aspect of observable with for example the async pipe, ngIf or the Elvis operator (?). 小心处理可观察的异步方面,例如异步管道,ngIf或Elvis运算符(?)。

<div>{{weather?.someProperty}}</div>

You could indeed make a class that models the json response and cast to it, or just use it as an any and use the dot-notation to extract and display the data. 您确实可以创建一个类来模拟json响应并转换为它,或者只是将其用作any并使用点符号来提取和显示数据。 Just add a field and assign the response to it. 只需添加一个字段并为其分配响应即可。 Like this: 像这样:

countryData: any;

getWeather(query) {
    const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98';
    return this.http
        .get(endpoint)//, {search: searchParams})
        .map(res => res.json().main)
        .subscribe(res => this.countryData = res)
        );
}

If you want to model it as a class beforehand you can do that too: 如果你想事先将它建模为一个类,你也可以这样做:

countryData: Country;

getWeather(query) {
    const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98';
    return this.http
        .get(endpoint)//, {search: searchParams})
        .map(res => res.json().main)
        .subscribe(res => this.countryData = <Country>res)
        );
}

Beware however that if you use the second method and cast to Country or whatever you name your class, that it won't have any methods available that you defined on that class. 但请注意,如果您使用第二种方法并强制转换为Country或您为其命名的任何名称,那么它将不具有您在该类上定义的任何可用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM