Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

angular - ionic2 ion-list with a button both having (click) event

What I am trying to achieve is when I click on the download button it should do somthing else and when clicking on the item it should open a new window

<ion-list>
    <ion-item *ngFor="let reading_material of reading_materials" (click)="gotoReadingMaterial(reading_material)">
        {{reading_material.title}}
        <ion-icon item-right name="download" (click)="downloadMaterial(reading_material)"></ion-icon>
    </ion-item>
</ion-list>

But when I click on the download button, both the events gets hit. Is there a way i can suppress the item event when i click on the download button ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can solve this issue by using event.stopPropagation();.

Please take a look at this plunker.

like you can see there, I also send the $event object to both methods

<ion-list>
    <ion-item *ngFor="let item of items" (click)="open($event, item)">
        {{ item }}
        <ion-icon (click)="download($event, item)" item-right name="download"></ion-icon>
    </ion-item>
</ion-list>

And then I use that information to stop the propagation of the event, so only the download method will be executed when clicking the download icon

  public open(event, item) {
    event.stopPropagation();
    alert('Open ' + item);
  }

  public download(event, item) {
    event.stopPropagation();
    alert('Download ' + item);
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...