gpt4 book ai didi

mysql - Angular Node.js mysql 喜欢不喜欢按钮

转载 作者:行者123 更新时间:2023-11-29 16:19:08 24 4
gpt4 key购买 nike

我正在为我的类似 twitter 的网站创建 YouTube 风格(喜欢/不喜欢)按钮。但我在实现喜欢-不喜欢按钮时遇到了问题,我正在使用 Angular Node.js MYSQL,带有 NgFor 循环和 ngIf 条件,我有两个表 posts{post_id, user_id,description} 和 likes{like_id, user_id, post_id, like_status } 带有公共(public)键 user_id, post_id。

如果我们在喜欢表中没有特定用户的任何值,我们如何编写条件来显示基本按钮

<div class="container" *ngFor="let post of posts; let i = index ">
<h6> {{post.description}} </h6>
<div class="row">
<div class="col-md-3">
<div *ngFor="let like of likes; ">

<div *ngIf="((post.user_id==like.user_id) && (post.post_id==like.post_id) && (like.like_status=='like'))">
<button type="button" class="btn btn-success" (click)=likeSubmit(post.user_id,post.post_id)><i class="fa fa-thumbs-up"
aria-hidden="true"></i></button>
</div>

<div *ngIf="((post.user_id==like.user_id) && (post.post_id==like.post_id) && (like.like_status=='unlike'))">
<button type="button" class="btn btn-warning" (click)=likeSubmit(post.user_id,post.post_id)><i class="fa fa-thumbs-up"
aria-hidden="true"></i></button>
</div>

<!---problem with ngIf condition -->
<div *ngIf="ngIf condition">
<button type="button" class="btn btn-basic" (click)=likeSubmit(post.user_id,post.post_id)><i
class="fa fa-thumbs-up" aria-hidden="true"></i></button>
</div>

前两个 ngIf 条件工作良好,但我无法为最后一个条件提供完美的 ngIf 条件,如果按钮 like_status == like,& like_status==unlike 工作良好,如果我们在 like 表中没有任何值如何我们可以写 ngIf 条件 o 显示按钮

最佳答案

您这样做有点不正确,您不需要在模板中执行内部循环,您可以在组件逻辑中执行两个表的连接,然后只渲染所需的数据。尝试将方法添加到连接两个表的组件中,然后您可以轻松地在模板中执行您想要的操作。示例:

Ts 文件

...
public getPostsWithLikes() {
this.posts.map(post => {
const like = this.likes && this.likes.find(plike => post.user_id === plike.user_id && post.post_id === plike.post_id);
return {post, like};
});
}
...

HTML 文件

<div class="container" *ngFor="let item of getPostsWithLikes(); let i = index ">
<h6> {{item.post.description}} </h6>
<div class="row">
<div class="col-md-3">
<div *ngIf="item.like && item.like.like_status=='like'">
<button type="button" class="btn btn-success" (click)=likeSubmit(item.post.user_id,item.post.post_id)><i class="fa fa-thumbs-up"
aria-hidden="true"></i></button>
</div>

<div *ngIf="item.like && item.like.like_status=='unlike'">
<button type="button" class="btn btn-warning" (click)=likeSubmit(item.post.user_id,item.post.post_id)><i class="fa fa-thumbs-up"
aria-hidden="true"></i></button>
</div>

<!---if you don't have related record in likes table, like would be null (false), so you just need to invert it-->
<div *ngIf="!item.like">
<button type="button" class="btn btn-basic" (click)=likeSubmit(item.post.user_id,item.post.post_id)><i
class="fa fa-thumbs-up" aria-hidden="true"></i></button>
</div>

希望有帮助。

关于mysql - Angular Node.js mysql 喜欢不喜欢按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54606745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com