简体   繁体   English

如何获取JSON数据并在Angular 8输入类型的组件HTML中显示

[英]How to get JSON data and show in component HTML with input type for Angular 8

EDIT 编辑

Hi, let me clear my below issue.. I have edited all the source code below, firstly I have two json file (one is from payment and other is from sale) but after checked with the API again.. I can use only one JSON with all the data showing. 嗨,让我清除下面的问题。.我编辑了下面的所有源代码,首先我有两个json文件(一个来自付款,另一个来自销售),但再次与API进行检查之后。.我只能使用一个显示所有数据的JSON。

Please check my revised JSON report and all the source code for your reff, my concern is when I put "pzrgb2l1lc8w7dp5" (from the payment object) in html.component, the table will be show "sale_id" & "status" (from the payment object) and "firstname", "lastname" & "email" (from the customer object with same "sale_id"). 请检查我修改过的JSON报告和所有reff的源代码,我担心的是,当我将“ pzrgb2l1lc8w7dp5”(来自付款对象)放在html.component中时,该表格将显示“ sale_id”和“ status”(来自付款对象)和“名字”,“姓氏”和“电子邮件”(来自具有相同“ sale_id”的客户对象)。

EDIT 编辑

previously apologize if this question already exists, but it seems not yet because it has been a week I looked for a solution to my problem and have not found the answer. 以前为这个问题已经存在而道歉,但似乎还没有,因为已经过了一周,我一直在寻找解决问题的方法,但没有找到答案。

I have managed to get JSON data from the HTTP service using angular, I have two JSON Urls and want to be made into one report and succeed. 我已经设法使用angular从HTTP服务获取JSON数据,我有两个JSON Urls,并且希望成为一份报告并成功。 But I want to use input and buttons to get the JSON report, I haven't found the tutorial. 但我想使用输入和按钮来获取JSON报告,但尚未找到本教程。

{
  "success": 1,
  "object": "list",
  "total_count": 2,
  "data": [
    {
      "object": "sale",
      "id": "j9cncjq0",
      "status": "Completed",
      "customer": {
        "object": "customer",
        "id": "uj56cbj3943sq1sg",
        "email": "iron.man@email.com",
        "firstname": "Iron",
        "lastname": "Man"
      },
      "payments": {
        "object": "list",
        "total_count": 1,
        "data": [
          {
            "object": "payment",
            "id": "pzrgb2l1lc8w7dp5",
            "sale_id": "j9cncjq0",
            "status": "COMPLETED",
          }
        ]
      }
    },
    {
      "object": "sale",
      "id": "sl8hcw26",
      "status": "Completed",
      "customer": {
        "object": "customer",
        "id": "upwvs7xqbc6zhwxh",
        "email": "black.widows@email.com",
        "firstname": "Black",
        "lastname": "Widows"
      },
      "payments": {
        "object": "list",
        "total_count": 1,
        "data": [
          {
            "object": "payment",
            "id": "pjd79f1yygqrm43q",
            "sale_id": "sl8hcw26",
            "status": "COMPLETED",
          }
        ]
      }
    }
  ]
}

Below is the json.service.ts code 以下是json.service.ts代码

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class JsonService {

  api_key = '1237bb46b22ee';

  private _urlSale: string = 'https://source-website-api/sales?apiKey='+this.api_key;

  constructor(private http: HttpClient) { }

  getSale() {
    return this.http.get(this._urlSale)
  }

}

Below is json.component.ts code 下面是json.component.ts代码

import { Component, OnInit } from '@angular/core';
import { JsonService } from '../../service/json.service';

@Component({
  selector: 'app-json',
  templateUrl: './json.component.html',
  styleUrls: ['./json.component.css']
})
export class JsonComponent implements OnInit {

  saleJSON: object;

  constructor(private _http: JsonService) { }

  ngOnInit() {
    this._http.getSale().subscribe(data => {
      this.saleJSON = data;
      console.log(this.saleJSON);
    })
  }
}

Below is json.component.html code 以下是json.component.html代码

<h1>JSON Receive Data</h1>

<p>Payment Number</p>

<input type="text"> <br><br>
<button type="submit">Check</button>
<p></p>

<table style="width:70%">
    <tr>
      <th>Sale ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
      <th>Status</th>
    </tr>
    <tr align="center">
      <td> payment.sale_id </td>
      <td> customer.firstname </td>
      <td> customer.lastname </td>
      <td> customer.email </td>
      <td> payment.status </td>
    </tr>
  </table> 

<p>{{errorMsg}}</p>

I hope there is someone who can help me, thank you. 我希望有人可以帮助我,谢谢。

If you are referring to making the value from the input accessible to the button click function, there are a couple ways to do this. 如果您指的是使按钮单击功能可访问输入中的值,则有两种方法可以执行此操作。 An easy option would be to create a variable for the input and bind to the html input element. 一个简单的选择是为输入创建变量并将其绑定到html input元素。 This value can then be accessed from your click button function. 然后可以通过单击按钮功能访问此值。 Eg 例如

  export class JsonService {
    inputValue: string
  ...

  check(){
    console.log('running check',this.inputValue)
  }
<input type="text" [(ngModel)]="inputValue"> <br><br>
<button type="submit" (click)="check()">Check</button>

Alternatively you can cut out a little bit of code by giving the input html a local reference and refer to it in the button click function 或者,您可以通过为输入html提供本地引用并在按钮单击功能中引用它来切掉一些代码

  check(value){
    console.log('running check',value)
  }
<input type="text" #myInput> <br><br>
<button type="submit" (click)="check(myInput.value)">Check</button>

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

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