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

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

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

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

This is the class

  1. export class TestComponent implements OnInit {
  2. constructor(private http: HttpClient) {
  3. this.http
  4. .get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
  5. console.log(data);
  6. });
  7. }
  8. ngOnInit() {
  9. this.http
  10. .get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
  11. console.log(data);
  12. });
  13. }
  14. }

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

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

This is the class

  1. export class TestComponent implements OnInit {
  2. constructor(private http: HttpClient ) {
  3. this.http
  4. .get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
  5. console.log(data);
  6. });
  7. }
  8. ngOnInit(){
  9. this.http
  10. .get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
  11. console.log(data);
  12. });
  13. }
  14. }

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... 循环遍历它:

  1. export class TestComponent implements OnInit {
  2. allData: any = null; // 或者你可以使用其他类型
  3. constructor(private http: HttpClient){}
  4. ngOnInit(){
  5. this.http.get<any>('http://api.data_redacted_for_privacy').subscribe(data => {
  6. console.log(data);
  7. this.allData = data;
  8. });
  9. }
  10. }

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

英文:

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

  1. export class TestComponent implements OnInit {
  2. allData: any = null; // or any other type you have at your disposal
  3. 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!)
  4. ngOnInit(){
  5. this.http.get&lt;any&gt;(&#39;http://api.data_redacted_for_privacy&#39;).subscribe(data =&gt; {
  6. console.log(data);
  7. this.allData = data;
  8. });
  9. }
  10. }

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:

确定