简体   繁体   English

如何在angular.template.html文件中以表形式返回JSON数据

[英]How do I return JSON data as a table in my template.html file in angular

I am a total web developer noob and somehow I have been assigned to writing some angular to produce these reports even though I have no idea what I'm doing (basically my employer wants to see if they can avoid making a new hire if they can avoid it). 我是一个Web开发人员,总的来说,即使我不知道自己在做什么,我还是被分配去编写一些角度来生成这些报告(基本上,我的雇主想知道他们是否可以避免聘请新员工)躲开它)。 Here is my component.ts file: 这是我的component.ts文件:

import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";

@Component({
  styleUrls: ["./styles.scss"],
  templateUrl: "./template.html"
})
export class MyRouteData {
  MyDataObject: object;

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http
      .get("http://localhost:54499/MyRoute/GetMyData")
      .subscribe(response => {
        console.log(response);
        this.MyDataObject = response;
      });
  }
}

Where the GetMyData endpoint basically runs the following query: GetMyData端点基本上在其中运行以下查询:

Select Top 20 CustomerId, FirstName, LastName, Address, PhoneNumber, Created From Customers Order by Created Desc

I can return the data to the page as a JSON with the following template.html file: 我可以使用以下template.html文件将数据作为JSON返回到页面:

<div style="background-color: white; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12); padding: 16px 8px 0 8px">
    <table>
        <tr>
            <td>{{MyDataObject|json}}</td>
        </tr>
    </table>
</div>

How do I return the data to the screen in a table? 如何将数据返回表中的屏幕?

I've made a few attempts and doing some loops but with no success. 我做了几次尝试,做了一些循环,但没有成功。

You need to use ngFor : 您需要使用ngFor

HTML HTML

    <div style="background-color: white; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12); padding: 16px 8px 0 8px">
        <table>
         <thead>
           give the table head columns
         </thead>
            <tr *ngFor="let data of MyDataObject">
                <td>{{data.name}}</td>
                <td>{{data.value}}</td>
            </tr>
        </table>
    </div>

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

相关问题 如何在我的代码后面的表中添加数据并以html显示? - How do I add data to a table in my code behind and display in html? html 模板未加载:找不到路径“/Helpers/Email/Templates/template.html”的一部分 - html template not loading: Could not find a part of the path '/Helpers/Email/Templates/template.html' 如何将文件表与 SQL Server 中的现有表链接 - How do I link the File Table with my existing table in SQL server 如何让我的getter从属性/元数据返回数据? - How do I have my getter return data from attribute/metadata? 如何将我的 MAUI 应用程序连接到 Firestore 数据库? (通过服务帐户 json 文件) - How do I connect my MAUI app to a Firestore database? (via service account json file) 如何正确反序列化json数据? - How do I properly deserialize json data? 如何使用 EF Core 正确更新表中的列表数据 - How do I properly update the list data in my table using EF Core 如何使用仅包含数字的字符串返回 JSON - How do I return JSON with strings that contain only numbers 如何在运行时设置视图/视图模型数据模板? - How do I set view/view model data template at runtime? 如何使用里面的文件返回我的文件夹列表? - How do I return a list of my folders with the files inside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM