[英]i am not able to access the properties of object in angluar
i am using getshipment method to get data by id which is return an object and displaying on frontend like this [object Object].
我正在使用 getshipment 方法通过 id 获取数据,它返回 object 并像这样 [object Object] 显示在前端。 i want to access the details and then display details on frontend
我想访问详细信息,然后在前端显示详细信息
export class ShipmentInfoComponent implements OnInit {
shipment: any[];
constructor(
private route: ActivatedRoute,
private location: Location,
private shipmentService: ShipmentService
) {}
ngOnInit(): void {
this.getShipment();
}
getShipment(): void {
const id = this.route.snapshot.paramMap.get('id');
console.log(id);
this.shipmentService
.getShipment(id)
.subscribe((shipment) => (this.shipment = shipment));
}
}
my getshipment method in sevices is我在服务中的装运方法是
getShipment(id: string): Observable<any[]> {
const shipmentUrl = `${this.url}/${id}`;
return this.http.get<any[]>(shipmentUrl);
}
html component of shipment info. html 组件的发货信息。 shipment.id or shipment.name is returning undefined
ship.id 或 shipping.name 返回未定义
<div class="shipment">
{{ shipment }}
</div>
object data object数据
{
"id": "S1000",
"name": "T-shirts(Summer2018) from Shanghai to Hamburg",
"cargo": [
{
"type": "Fabric",
"description": "1000 Blue T-shirts",
"volume": "2"
},
{
"type": "Fabric",
"description": "2000 Green T-shirts",
"volume": "3"
}
],
"mode": "sea",
"type": "FCL",
"destination": "Saarbrücker Str. 38, 10405 Berlin",
"origin": "Shanghai Port",
"services": [
{
"type": "customs"
}
],
"total": "1000",
"status": "ACTIVE",
"userId": "U1000"
},
You cannot print object directly, You have to print whatever value you want to print.您不能直接打印 object,您必须打印要打印的任何值。 like below,
如下所示,
If you are receiving array in response, then use below如果您收到数组作为响应,请在下面使用
<div class="shipment" *ngFor="let ship of shipment">
Name: {{ ship.name }}
<br>
Mode: {{ship.mode}}
</div>
Otherwise, Change your service method return type array to object like this,否则,像这样将您的服务方法返回类型数组更改为 object,
getShipment(id: string): Observable<any> {
const shipmentUrl = `${this.url}/${id}`;
return this.http.get<any>(shipmentUrl);
}
and html code will be like this,和 html 代码将是这样的,
<div class="shipment" *ngIf="shipment">
Name: {{ shipment?.name }}
<br>
Mode: {{shipment?.mode}}
</div>
You can refer this link from Angular.io您可以从Angular.io参考此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.