简体   繁体   中英

ion-card function call (onclick)

Using the ionic 5.0.2 version I am trying to handle an event click inside an ion-card. The bad thing is that I am not able to handle the single event with the single function you are thinking right now... Take a look 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>

As you can see above I have a function called greed, I want to send a value to the function so I know which card is being clicked, below you can see the module:

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);

  }
}

Even if I add no parameters and just type a console.log("HI stackoverflow"); I am not being able to get the click, any ideas?

instead of

(onclick)="greed($event)"

use

(click)="greed($event)" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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