Attempting to handle a click event within an ion-card using Ionic 5.0.2 version has presented some challenges. Despite my efforts, I have not been successful in handling the event with the expected function. Here is a snippet of my code:
Dynamic card list home.page.html
<ion-card *ngFor="let jsons of json" button (onclick)="greed($event)">
<ion-item>
<ion-avatar slot="start">
<img src="http://icons.iconarchive.com/icons/papirus-team/papirus-status/256/avatar-default-icon.png">
</ion-avatar>
<ion-label>{{jsons.nombre_servicio}}</ion-label>
</ion-item>
<ion-card-content >
<ion-grid>
<ion-row>
<ion-col size="8" size-md>
{{jsons.serviciodescripcion}}
</ion-col>
<ion-col size="4">
<ion-chip>
<ion-icon name="pin"></ion-icon>
<ion-label>{{jsons.costoHora}}</ion-label>
</ion-chip>
</ion-col>
</ion-row>
</ion-grid>
</ion-card-content>
</ion-card>
In the above code snippet, I have a function named greed that is supposed to receive a value indicating which card was clicked. The corresponding module is shown below:
home.page.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AlertController } from '@ionic/angular';
@Component({
selector: 'app-home-home',
templateUrl: './home-home.page.html',
styleUrls: ['./home-home.page.scss'],
})
export class HomeHomePage implements OnInit {
user: any;
constructor(public httpClient: HttpClient, public alertController: AlertController, public router: Router) { }
json : any;
clicked: any;
ngOnInit() {
const token = 'top10';
this.httpClient.get(`https://mydatabaseapi/servicios.php?&token=${token}`)
.subscribe(async data => {
if ( data == null) {
} else {
console.log(data);
this.json = data;
}
}, error => {
console.log(error);
});
}
back() {
this.user = history.state;
this.router.navigate(['/menu/:user'], {state: this.user});
}
greed(obj: any) {
console.log(obj);
}
}
Despite trying various approaches, including using console.log("HI stackoverflow"); without any parameters, I have not been able to capture the click event. Any suggestions?