I have this array of objects which holds bunch of comments:
comments: Comment[];
And this array is filled up inside ngOnInit() function by invoking service code. Later on, there is a key in my comments array which is commentPlus among with other keys. This value represents the total upvotes for a comment. I use its value inside my html like this:
<div class="media mb-3" *ngFor="let comment of comments; trackBy: trackByFn">...
<a class="btn btn-success" (click)="upVote()" [ngClass]="{'active' : comment.voted > 0 }"><i class="fa fa-thumbs-up"></i> {{comment.commentPlus}}</a>
What I want is to update the value {{comment.commentPlus}} and style of the button whenever user hits the button. Below is the code for upVote() function
upVote(commentId: number, userId: number){
userId = this.userId;
commentId = 4; //I just assigned some number that I know it exists in my database for testing.
this.service.upVote(commentId, userId).subscribe();
}
But the problem is that neither style change that should be performed by [ngClass]="{'active' : comment.voted > 0 }" nor new value for {{comment.commentPlus}} reflects in front end. Backend is working as it should be, but these two changes require page to be refreshed.
export class Yorum {
commentId?: number;
userId?: number;
comment?: string;
commentPlus?: number;
commentMinus?: number;
voted?: number;
}