如何从响应对象中一个接一个地获取JSON对象并在Angular中显示在表中?



JSON数组作为响应对象从我的Java应用程序进入typescript。我需要选择每个对象,并在angular中显示为对应typescript页面的html页面。
list-user.component.ts

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
@Component({
selector: 'app-list-user',
templateUrl: './list-user.component.html',
styleUrls: ['./list-user.component.css']
})
export class ListUserComponent implements OnInit {
loginForm!: FormGroup;
submitted = false;
jdbc: any;
username:any
constructor(private http: HttpClient, private router: Router) { }
ngOnInit() {
this.username = JSON.parse(localStorage.getItem("session") || "");
let listuser: any = {
username: this.username,
}
this.http.post('http://localhost:8080/demoprojectjava/list-user/',
listuser, { observe: 'response' }).subscribe(res => {
console.log(res);
});
}
}

list-user.component.html

<div>
<h1 class="listuser"> Display Data from Json File </h1>
<table>
<thead>
<tr>
<th>User Id</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{--Here I need value from Json array}}</td>
<td>{{--Here I need value from Json array}}</td>
</tr>
</tbody>  
</table>
</div>

当我运行我的代码时,在控制台中我得到了console.log(res);的输出结果:

{"user_data":[{"user_id":"111","username":"Hridya"},{"user_id":"222","username":"Rizz"}]}

将http请求作为Observable保存在属性中(这里是userData$)。然后使用async管道在你的模板有角自动subscribe到它。然后使用ngFor遍历响应中的所有元素,并相应地显示数据。

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
type ResponseTypeFromYourBackend = any;
@Component({
selector: 'app-list-user',
templateUrl: './list-user.component.html',
styleUrls: ['./list-user.component.css']
})
export class ListUserComponent implements OnInit {
loginForm!: FormGroup;
submitted = false;
jdbc: any;
username:any
userData$: Observable<ResponseTypeFromYourBackend>;
constructor(private http: HttpClient, private router: Router) { }
ngOnInit() {
this.username = JSON.parse(localStorage.getItem("session") || "");
let listuser: any = {
username: this.username,
}
this.userData$ = this.http.post('http://localhost:8080/demoprojectjava/list-user/',
listuser,
{ observe: 'response' }
);
}
}

<div>
<h1 class="listuser"> Display Data from Json File </h1>
<table *ngIf="userData$ | async as data">
<thead>
<tr>
<th>User Id</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of data.user_data">
<td>{{ user.user_id}}</td>
<td>{{ user.username}}</td>
</tr>
</tbody>
</table>
</div>

最好的方法是这样做:

  1. 在类级别定义一个可观察对象:
userData$: Observable<YourType>;
...
ngOnInit() {
...
this.userData$ = this.http.post('http://localhost:8080/demoprojectjava/list-user/',
listuser).pipe(map(item => item.user_data));
}
  1. 使用async管道订阅这个可观察对象并遍历数组:
<tr *ngFor="let item of userData$ | async">
<td>{{item.user_id}}</td>
<td>{{item.username}}</td>
</tr>

创建一个变量来存储响应,并使用ngFor循环遍历数组。

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
@Component({
selector: 'app-upload-active-census',
templateUrl: './upload-active-census.component.html',
styleUrls: ['./upload-active-census.component.css'],
})
export class UploadActiveCensusComponent implements OnInit {
loginForm!: FormGroup;
submitted = false;
jdbc: any;
username: any;
data: any;
constructor(private http: HttpClient, private router: Router) {}
ngOnInit() {
this.username = JSON.parse(localStorage.getItem('session') || '');
let listuser: any = {
username: this.username,
};
this.http
.post('http://localhost:8080/demoprojectjava/list-user/', listuser, {
observe: 'response',
})
.subscribe((res) => {
console.log(res);
return this.data = res;
});
}
}
<div>
<h1 class="listuser"> Display Data from Json File </h1>
<table>
<thead>
<tr>
<th>User Id</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of data?.user_data">
<td>{{item.user_id}}</td>
<td>{{item.username}}</td>
</tr>
</tbody>
</table>
</div>

@毗湿奴帕布。谢谢你的回答。
你的Typescript代码工作得很顺利…但我没有得到价值与你的html代码和做了一个小的改变。然后我得到了表格中的值。所以我发布了整个代码,为我工作得很好,其他人供将来参考。
list-user.component.ts

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
@Component({
selector: 'app-list-user',
templateUrl: './list-user.component.html',
styleUrls: ['./list-user.component.css']
})
export class ListUserComponent implements OnInit {
loginForm!: FormGroup;
submitted = false;
jdbc: any;
username: any;
data: any;
constructor(private http: HttpClient, private router: Router) { }
ngOnInit(): void {
this.username = JSON.parse(localStorage.getItem("session") || "");
let listuser: any = {
username: this.username
}
this.http.post('http://localhost:8080/demoprojectjava/list-user/',
listuser, { observe: 'response' }).subscribe(res => {
console.log(res.body);
return this.data = res;
},
);
}
}

list-user.component.html

<div>
<h1 class="listuser"> Display Data from Json File</h1>
<table>
<thead>
<tr>
<th>User Id</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of data.body.user_data">
<td>{{user.user_id}}</td>
<td>{{user.username}}</td>
</tr>
</tbody>
</table>
</div>

相关内容

  • 没有找到相关文章

最新更新