I realise this might be a duplicate of this or this, but i've gotten blind to this. Im getting my data from a rest api as an object, which contains number of votes. The toggle function works on all the fontawesome icons, and not just the specific ones, even though the counters are working fine. I want the filled heart icon to be toggled on the specific story, when the icon has been pressed. Vice versa when it's been pressed again.
I'm using angular4 and fontawesome 5.
The data is from an observable in my service.
Component:
private vote(story) {
this.liked = !this.liked; //heart filled
if (story.userVote == false) {
this.storiesService.voteStory(story.objectID)
.subscribe(
(data) => {
console.log("Added upvote")
story.votes++; //api counter
console.log(story.votes)
story.userVote = true; //api call
})
} else if (story.userVote == true) {
this.storiesService.unVoteStory(story.objectID)
.subscribe(
(data) => {
console.log("Removed upvote")
story.votes--;
console.log(story.votes)
story.userVote = false;
})
}
}
HTML:
<a (click)="vote(story)" style="font-size: 13px;" *ngIf="!liked" matTooltip="Like story" matTooltipPosition="above"><i class="far fa-heart"></i> {{ story.votes || 0 }} </a>
<a (click)="vote(story)" style="font-size: 13px;" *ngIf="liked" matTooltip="Like story" matTooltipPosition="above"><i class="fas fa-heart"></i> {{ story.votes || 0 }} </a>