I’m trying to toggle button to add an item, feed here, to favorite list. the problem here when i click on any button other buttons are fired in the same time? is there a way to stop this behavior?!
this is my code
Template
<ion-content padding>
<ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>
<ion-list>
<ion-list-header>
<h2>News</h2>
</ion-list-header>
<ion-item *ngFor=" let feed of feeds; let i = index " (click)="onFeedSelected(feed)">
<ion-avatar item-left>
<img src="{{feed.img}}">
</ion-avatar>
<h2>{{ feed.title }}</h2>
<button ion-button [color]="isMyFav ? 'secondary' : 'primary'" outline text-center icon-right item-right (click)="addToFavList($event, feed)">
<ion-icon [name]="isMyFav ? 'checkmark' : 'add' "></ion-icon>
</button>
</ion-item>
</ion-list>
</ion-content>
Component
export class HomePage {
feeds: Feed[] = [];
myFavList: Feed[] = [];
isMyFav = false;
constructor(public navCtrl: NavController, private feedService: FeedService) {
}
ionViewDidLoad() {
this.feeds = this.feedService.loadFeed();
}
onFeedSelected(feed: Feed) {
this.navCtrl.push(FeedListPage, feed);
}
addToFavList($event, feed) {
$event.stopPropagation();
$event.preventDefault();
let index = this.feeds.indexOf(feed);
console.log(index);
if (!this.isMyFav) {
if (this.myFavList.indexOf(feed) == -1) {
this.myFavList.push(feed);
this.isMyFav = true;
}
} else {
if (this.myFavList.indexOf(feed) != -1) {
this.myFavList.splice(index, 1);
this.isMyFav = false;
}
}
console.log(this.myFavList);
}
}