[英]How to get JSON data and show in component HTML with input type for Angular 8
嗨,让我清除下面的问题。.我编辑了下面的所有源代码,首先我有两个json文件(一个来自付款,另一个来自销售),但再次与API进行检查之后。.我只能使用一个显示所有数据的JSON。
请检查我修改过的JSON报告和所有reff的源代码,我担心的是,当我将“ pzrgb2l1lc8w7dp5”(来自付款对象)放在html.component中时,该表格将显示“ sale_id”和“ status”(来自付款对象)和“名字”,“姓氏”和“电子邮件”(来自具有相同“ sale_id”的客户对象)。
以前为这个问题已经存在而道歉,但似乎还没有,因为已经过了一周,我一直在寻找解决问题的方法,但没有找到答案。
我已经设法使用angular从HTTP服务获取JSON数据,我有两个JSON Urls,并且希望成为一份报告并成功。 但我想使用输入和按钮来获取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",
}
]
}
}
]
}
以下是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)
}
}
下面是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);
})
}
}
以下是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>
我希望有人可以帮助我,谢谢。
如果您指的是使按钮单击功能可访问输入中的值,则有两种方法可以执行此操作。 一个简单的选择是为输入创建变量并将其绑定到html input元素。 然后可以通过单击按钮功能访问此值。 例如
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>
或者,您可以通过为输入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.