angular - HTTP响应以角度打印为[object Object],[object Object]
问题描述
这是我的 price.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class PriceService {
constructor(private http: HttpClient) { }
getProduct(): Observable<any> {
return this.http.get('http://localhost:8080/api/v1/products');
}
}
这是 price-list.component.ts
import { Component, OnInit } from '@angular/core';
import {FormControl} from '@angular/forms';
import { PriceService } from './price.service';
@Component({
selector: 'app-price-list',
templateUrl: './price-list.component.html',
styleUrls: ['./price-list.component.css']
})
export class PriceListComponent implements OnInit {
disableSelect = new FormControl(false);
selectedValue :any
sessions: any=[]
constructor(private priceService: PriceService) { }
ngOnInit(): void {
this.priceService.getProduct().subscribe((data: any) => {
this.sessions = Object.values(data);
});
}
}
我尝试在我的 price-list.component.html 中打印响应,如下所示
{{会话}}
但它打印在下面
[对象对象],[对象对象]
这是我的邮递员输出
[ {“id”:8,“name”:“企鹅耳朵”,“cartonPrice”:175,“unitPrice”:9.0,“unitForCarton”:20 },{“id”:9,“name”:“企鹅-earsghhc”,“cartonPrice”:175,“unitPrice”:9.0,“unitForCarton”:20 }]
如何打印实际数据
解决方案
假设您希望将其显示为 html ul 元素。然后,您可以按以下方式进行:
<ul>
<ng-container *ngFor="let sessionItem of sessions">
<li *ngFor="let keyVal of sessionItem | keyvalue"> {{ keyVal.key }}: {{ keyVal.value }}</li>
</ng-container>
</ul>
推荐阅读
- flutter - flutter table 日历事件列表
- coq - Coq:如何证明 forall nm, n <= m \/ m <= n
- php - PHP SQL Server (MS) 停止返回带有特殊字符的行
- reactjs - 无法从 useContext 访问对象值。使用 useReducer 和 useContext 进行状态管理
- python - Pandas read_excel 忽略了我需要保留的空白列
- c# - 调整大小时出现滞后的 C#
- javascript - 使用 Array 的 forEach 方法用 JS 构建导航菜单和无序列表
- java - 大摇大摆地只显示选定的字段
- kotlin - SonarQube 代码覆盖率不适用于包含各种类的 Kotlin 文件
- python - 如何以编程方式将小部件添加到中央小部件(由 pyside2-uic 生成的 ui)?