从HttpClient Rest Api中如何插值数据?

huangapple go评论62阅读模式
英文:

How can I Interpolate data from HttpClient Rest Api?

问题

I have this component where I am pulling data from a REST API using the HttpClient. The pulling of the data works well. I could tell because I could see the data being displayed in Console Log.

When it comes to using the data in the HTML I am stuck. I can't figure how to do so. Nothing is broken; the Data just won't show.

Here is my Code

Imports in the Component

import { Component, OnInit } from '@angular/core';
import { MatTabsModule } from '@angular/material/tabs';
import { MatListModule } from '@angular/material/list';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';

This is the class

export class TestComponent implements OnInit {
  constructor(private http: HttpClient) {

    this.http
      .get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
        console.log(data);
      });
  }

  ngOnInit() {

    this.http
      .get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
        console.log(data);
      });

  }
}

The HTML is as follows

<div *ngFor="let data">{{data.id}}</div>

I tried adding an Observable, but I believe my implementation is wrong. Sure, it's not like using AngularFire.

英文:

I have this component where I am pulling data from a REST API using the HttpClient. The pulling of the data works well. I could tell because I could see the data being displayed in Console Log.

When it comes to using the data in the HTML I am stuck. I can't figure how to do so. Nothing is broken the Data just won't show.

Here is my Code

Imports in the Component

    import { Component, OnInit } from &#39;@angular/core&#39;;
    import {MatTabsModule} from &#39;@angular/material/tabs&#39;;
    import {MatListModule} from &#39;@angular/material/list&#39;; 
    import { Observable } from &#39;rxjs&#39;;
    import {HttpClient, HttpHeaders} from &#39;@angular/common/http&#39;;

This is the class

    export class TestComponent implements OnInit {
    constructor(private http: HttpClient ) {
    
    this.http
    .get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
    console.log(data);
    });
    }

    ngOnInit(){

    this.http
    .get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
    console.log(data);
    });

    }
    }

The HTML is as follows

&lt;div *ngFor=&quot;let data&quot;&gt;{{data.id}}&lt;/div&gt;

I tried adding and Observable, but i believe my implementation is wrong. Sure is not like using AngularFire.

答案1

得分: 1

将返回的数据分配给一个类变量,然后使用 let... of... 循环遍历它:

export class TestComponent implements OnInit {

   allData: any = null; // 或者你可以使用其他类型

   constructor(private http: HttpClient){}

   ngOnInit(){

       this.http.get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
          console.log(data);
          this.allData = data;
        });
    }

}

然后在模板中使用:<div *ngFor="let data of allData">{{data.id}}</div>

英文:

Assign the returned data to a class variable, then loop through it using let... of...:

export class TestComponent implements OnInit {

   allData: any = null; // or any other type you have at your disposal

   constructor(private http: HttpClient){} // you don&#39;t need to subscribe to the same endpoint (if it&#39;s the same endpoint, disregard the comment if it&#39;s not!)

   ngOnInit(){

       this.http.get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
          console.log(data);
          this.allData = data;
        });
    }

}

Then in template: &lt;div *ngFor=&quot;let data of allData&quot;&gt;{{data.id}}&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月7日 02:49:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定