我想做一個功能,用戶可以喜歡評論類似instagram可以像其他職位一樣。元素在* ngFor
<div *ngFor="let comment of comments | orderBy: '-'+orderBy">
<div class="comment-item" >
<div class="row">
<div class="col-md-8">
{{comment.userName}}
</div>
<div class="col-md-4">
{{comment.time | timeAgo}}
<i class="fa fa-heart-o" aria-hidden="true" (click)="likeComment(comment._id)"
[@notliked]="notliked"></i>
<i class="fa fa-heart" aria-hidden="true" (click)="unlikeComment(comment._id)"
[@liked]="liked"></i>
{{comment.like}}
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="comment-content">{{comment.textArea}}</p>
</div>
</div>
</div>
然後創建在組分來控制哪些圖標應該顯示兩個可變。
notliked = 'open';
liked = 'close';
likeComment(commentId) {
this.liked = 'open';
this.notliked = 'close';
this.dataService.likeComment(commentId).subscribe(
res=>{
},
error => console.log(error)
)
}
unlikeComment(commentId) {
this.liked = 'close';
this.notliked = 'open';
this.dataService.unlikeComment(commentId).subscribe(
res=>{
},
error => console.log(error)
)
}
我的問題是* ngFor的所有循環項目在同一時間改變,我希望只是改變選定的一個。我知道我不能使用全局變量,但如何綁定在* ngFor中單獨創建的元素的屬性?謝謝!
你在用'comment._id'傳遞給'likeComment(...)''''和'similarComment(...)''嗎? –
對不起,我錯過了一些東西,然後再次編輯。我發表comment._id服務器然後功能好吧:) – PaulHuang