简体   繁体   English

以 angular 的形式将 ID 传递给子组件

[英]Passing an ID to a child component in angular

I have three components我有三个组成部分

A list component that does a request to receive an object, then a card component that is iterated for each object passed onto an array from the List component, lastly, I have a Detail component which im trying to pass an ID which is present in the object that is passed from the List component to the Card component.一个列表组件执行接收对象的请求,然后是一个卡片组件,该组件针对从 List 组件传递到数组的每个对象进行迭代,最后,我有一个 Detail 组件,它试图传递存在于从 List 组件传递到 Card 组件的对象。

I dont know how to pass this id.我不知道如何传递这个 ID。

List Component列表组件

this.webService.getObjects().subscribe((res : any) =>  {
      console.log(res) 
      this.cards = res.results  <-- An Array
      console.log(res.results)
      
    })

List Template:列表模板:

<app-card class="col-md-4" *ngFor="let card of cards"
    [card]={card}></app-card> <-- object passed to Card Component

Card Component:卡片组件:

@Input() 
  game : any 

Card Template:卡片模板:

 <a href="javascript: void" [routerLink]="['',card.card.name]" class="btn btn-primary align-items-center">Detail</a>

How do I pass an ID that inside an object that is passed from the List Component into the Card Component?如何传递从列表组件传递到卡片组件的对象内部的 ID?

我认为您正在卡片输入参数中传递 id,因此您可以使用 (?) 访问 html 中的卡片数据,以避免尝试访问无效引用。

 <a href="javascript: void" [routerLink]="['',card?.id]"   class="btn btn-primary align-items-center">Detail</a>

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

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