简体   繁体   English

如何使用 angular 材料表显示来自 API 响应的 object?

[英]How to display the object coming from API response using angular material table?

I have an object coming from response which looks something like .我有一个来自响应的 object,看起来. It's a dynamic key value pair这是一个动态键值对

{
  "name1": "value1",
  "name2": "value2",
  "name3": "value3"
  ............
  ............
}

I want to generate a material table from this.我想由此生成一个材料表。 I want to know how to get the response into my component file.我想知道如何将响应放入我的组件文件中。

I'm getting the response in my service file.我在我的服务文件中得到响应。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { map } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class ApiService {
  private headers = new Headers({ 'Content-Type': 'application/json' });  
  url: string = '';  
  constructor(private http:HttpClient) {
    this.url = "https://beta.randomapi.com/api/3141b5683af0edf576dabdb50ef1ff64?fmt=prettyraw&sole"; 
   }
   public getData() {  
    return this.http.get(this.url )  
      .pipe(map((res: Response) => {  
        return res.json();  
      })); 

  }  


}

First the datasource must be a array and the json you mentioned should be representing a row in table.首先,数据源必须是一个数组,您提到的 json 应该代表表中的一行。 Here is a sample example to use this.这是一个使用它的示例 This will give you a direction to move forward.这会给你一个前进的方向。

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

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