'pass data to child component when clicked on card in parent component ( card has image name id and more info ) angular

I want to pass data when clicked on the card(card has images, id, name, and more) from the parent(nfts) to the child(main) component and render the information in the child component.

I tried to use @Input() but I don't get the value on the child component, and also tried using the EventEmitter but I could not pass the value via the variable.emit(value) or is there a better way achieve this

nft.html

<app-main [nftId]="ItemClicked"></app-main>
<div class="container">
    <div class="collectionCard" *ngFor="let data of nftData" 
    (click)="getId(data.token_id,data.image_url,data.name)">
        <img src="{{data.image_url}}" alt="nf img" >
        <div class="details">
            <div class="name">
                {{data.name}} 
                <div class="id">#. {{data.token_id}}</div>
            </div>
        </div>
    
        <div class="priceContainer">
            <img src="assets/assets/weth.png" alt="symbol" class="weithImage">
            <div class="price">{{data.traits[0]?.value}}</div>
        </div>
    </div>
</div>

nft.ts

export class NftsComponent implements OnInit {
  nftData:any =[];
  ItemClicked:any = []; 
 
  getdata(){
    fetch('https://testnets-api.opensea.io/api/v1/assets?asset_contract_address=0x617d411DE5a4D5b668EBAa22Edc7bCdbb88285c4&order_direction=desc&offset=0&limit=5')
   .then(response =>response.json())
   .then(response =>{
     const data = response.assets;
     this.nftData = data;
     console.log(this.nftData);
   })
   .catch(err =>console.error(err))
  }

  getId(id:any,img:string,name:string){
    this.ItemClicked = id; 
    this.ItemClicked = img;
    this.ItemClicked = name;
  }
  
  constructor() { }

  ngOnInit(): void {
   this.getdata();
    
  }

main.ts

export class MainComponent implements OnInit {
  @Input() nftId:any;

  constructor() { }


  ngOnInit(){
    console.log( this.nftId);
  }
}

I want to receive data in the main.ts

stackbliz link - https://github.com/HemanthGirimath/nft-cryptoPunk-clone



Solution 1:[1]

You need to add implement ngOnchanges method inside the child component

export class MainComponent implements OnInit,OnChanges{
  @Input() nftId:any;

  constructor() { }


  ngOnInit(){
    console.log( this.nftId);
  }

ngOnChanges(change: SimpleChanges) {
    if(change["nftId"]) {
      console.log(change["nftId"]);
    }
  }

}

Solution 2:[2]

First declare Object

ItemClicked  : any;

getId(id:any,img:string,name:string){
    this.ItemClicked.id = id; 
    this.ItemClicked.img = img;
    this.ItemClicked.name = name;
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2