在ngFor循环内使用ngIf条件的类似Angular的按钮实现

时间:2019-05-08 09:33:22

标签: mysql node.js angular ngfor

我正在为我的网站使用YouTube样式(不喜欢)按钮。但是我不知道该怎么做。在我的数据库中,我有两个表帖子(post_id,user_id,描述),likes(like_id,user_id,post_id,like_status)列值。我加入了两个表,并通过user_id得到了值,我只是使用ngFor帖子并实现了ngIf条件,如果user_id在Likes表中,则它们可以正常工作。如果在likes表中不存在user_id,则无法显示按钮,因为我不知道默认状态下按钮的确切条件。任何人都可以帮助我。

我已经尝试了以下解决方案,但没有成功
 *ngIf="( (postLikes.length != 0) && (post.user_id == like.user_id) && (post.post_id == like.post_id) && (like.like_status == 'like'))"

*ngIf="( (postLikes.length != 0) && (post.user_id == like.user_id) && (post.post_id == like.post_id) && (like.like_status == 'unlike'))"

*ngIf="( (postLikes.length == 0) && (post.user_id !== like.user_id) && (post.post_id !== like.post_id) && (like.like_status !== 'unlike'))"

前两个ngIf条件正常,但最后一个ngIf条件无效。

<div class="container" style="width: 100%; height: 100%;  ">
  <div class="row" style=" margin: 1px; background-color: #fff; border: 2px solid #ada5a5; border-radius: 4px; ">
    <!-- ngFor for posts -->
    <div class="container" *ngFor="let post of posts; let i = index">
      <div class="row" style="border-top: 2px solid #ada5a5;">
        <div class="col-md-12 col-md-offset-0" style=" height: auto; ">
          <h6> {{post.description}} </h6>
        </div>
      </div>
      <!--ngFor for likes -->
      <div class="container" style="border: 0px solid #ada5a5; ">
        <div class="row">

          <!--like button-->
          <div class=" col-4">
            <div *ngFor="let like of postLikes; let j = index ">
              <div *ngIf="( (postLikes.length != 0) && (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)>Like</button><p>liked</p>
              </div>
              <div *ngIf="( (postLikes.length != 0) && (post.user_id == like.user_id) && (post.post_id == like.post_id) && (like.like_status == 'unlike'))">
                <button type="button" class="btn btn-danger" (click)=likeSubmit(post.user_id,post.post_id)>Like</button><p>Disliked</p>
              </div>

              <div
                *ngIf="( (postLikes.length == 0) && (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)>Like</button><p>Default</p>
              </div>

            </div>
          </div>

          <!-- dislike button -->
          <div class=" col-6">
            <div class="" *ngFor="let like of postLikes">

              <div *ngIf="((post.user_id==like.user_id) && (post.post_id==like.post_id) && (like.like_status=='like'))">
                <button type="button" class="btn btn-warning" (click)=likeSubmit(post.user_id,post.post_id)>Dislike</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-success" (click)=likeSubmit(post.user_id,post.post_id)>Disliked</button>
              </div>
            </div>
          </div>
        </div>
      </div><!--Like button ends-->     
  </div>
</div>
</div>

<TypeScript>

export class AppComponent  {
  title = 'my-app';
  name = 'Angular';

  posts: any[] =
    [{"post_id":4,"user_id":2,"description":" Hi How are you ","created_date":"2019-01-28T12:30:49.000Z"},{"post_id":5,"user_id":2,"description":" Working a Fine ","created_date":"2019-01-28T12:31:20.000Z"},{"post_id":6,"user_id":2,"description":" Hi How are you ......","created_date":"2019-01-28T12:32:15.000Z"},{"post_id":7,"user_id":2,"description":" 4th test post","created_date":"2019-01-29T07:10:37.000Z"},{"post_id":9,"user_id":2,"description":" 5th test post","created_date":"2019-01-31T11:17:31.000Z"},{"post_id":10,"user_id":2,"description":" 6th test post","created_date":"2019-01-31T12:03:54.000Z"},{"post_id":11,"user_id":2,"description":" 7th post post","created_date":"2019-02-08T05:50:02.000Z"},{"post_id":12,"user_id":2,"description":" 8th test post ","created_date":"2019-02-08T06:08:01.000Z"}];

  postLikes:any[] =[{"post_id":4,"user_id":2,"like_status":"unlike","like_id":10},{"post_id":5,"user_id":2,"like_status":"like","like_id":9},{"post_id":6,"user_id":2,"like_status":"like","like_id":8},{"post_id":7,"user_id":2,"like_status":"like","like_id":7},{"post_id":9,"user_id":2,"like_status":"like","like_id":11}]

  post_id: any;
  // likes: Like[];
  like_id: number | null ;
  like_status: string;


  constructor(private http: HttpClient, private formBuilder: FormBuilder){


  }

  ngOnInit() {   }

  // Get user details from DB
  getPosts(user_id) {
    this.userService.getPosts(user_id).subscribe((data) => {
      this.posts = data;
    },
      error => {
        return console.log(error);
      }
    );
  }

// join postlikes
  getPostLikes(user_id) {
    // debugger
    this.userService.get_PostLikes(user_id).subscribe((results) => {
      this.postLikes = results;
     // console.log(results, 'results', this.postLikes, 'likes');
    },
      error => {
        return console.log(error);
      }
    );
  }


当user_id不在点赞表中时,我希望ngIf条件显示默认按钮。我的stackBlitz链接 https://stackblitz.com/edit/angular-wddupe?file=src%2Fapp%2Fapp.component.ts

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,我只是无法添加评论,所以...
首先,'postLikes === 0'总是返回 False ,因为'postLikes'是一个数组,但是'0'是数字类型。
如果要检查数组中是否有任何值,则可以检查其长度
f.e. if(postLikes.length === 0)或更简洁的方式 if(postLikes.length)
另请了解'=''==''==='和有什么区别,并修复您的其他ngIf语句。
https://www.w3schools.com/js/js_operators.asp

据我了解,您将获得特定用户喜欢或不喜欢的所有post_id -s,因此请遍历postLikes数组,如果没有该特定的post_id,则显示默认的“喜欢”和“不喜欢”按钮。

我也建议重构整个逻辑。