使用ngIf else角度比较两个数组值以显示按钮

时间:2019-05-15 06:02:48

标签: arrays node.js angular

我正在使用ngIf else条件处理两个数组,我从两个表post [post_id,user_id,描述],likes [post_id,user_id,like_status,like_id]中获得了两个数组值。在这里,我需要比较post_id,user_id和like_status ='like'如果它们相等,则条件为true显示成功按钮,否则为false默认按钮。

我已经尝试过,但是无法正常工作,因此请在使用ngIf else时帮助比较两个数组。


<div class="container" *ngFor="let post of posts; let i = index">
   <h6> {{post.description}} </h6>

    <div class="container" style="border: 0px solid #ada5a5; ">
        <div class="row">

          <!--like button-->
          <div class=" col-4">

              <div *ngIf="(postLikes.indexOf(user_id) > -1) && (post.post_id == postLikes.post_id) && (postLikes.like_status == 'like'); else default">
                <button type="button" class="btn btn-success" (click)=likeSubmit(post.user_id,post.post_id)>Liked</button><p>liked</p>
              </div>
                <ng-template #default>
                  <button type="button" class="btn btn-secondary" (click)=likeSubmit(post.user_id,post.post_id)>Like</button><p>like</p>
                </ng-template>
         </div>

        </div>

      </div>


  </div>


// two tables data
  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;
  user_id:Number = 2;
  // likes: Like[];
  like_id: number | null ;
  like_status: string;





请尝试一次我的StackBlitz代码并更正错误

https://stackblitz.com/edit/angular-wddupe?file=src%2Fapp%2Fapp.component.ts

3 个答案:

答案 0 :(得分:1)

我已更新代码,请查找更改

https://stackblitz.com/edit/angular-yjcpfk?file=src/app/app.component.html

希望这对您有所帮助。

答案 1 :(得分:0)

将字符串与数组进行比较时,html中的点赞条件存在问题  Ngfor保留了postlikes数组

我建议您在typescript中使用find关键字进行此操作,以比较结果并将结果传递给布尔对象(例如显示的布尔对象)并隐藏按钮

答案 2 :(得分:0)

一种方法

  1. 在组件中创建一个公共方法,

    public isPostLikedByUser(postID: any, userID: any){
        // This is actually matching the criteria exactly as you have described in question RETURN true or false
        return this.postLikes.findIndex((i) => i.post_id == postID && i.user_id == userID && i.like_status == 'like') > -1;
    }
    
  2. 现在在您的视图中将此方法调用为

     <div *ngIf="isPostLikedByUser(post.post_id, post.user_id); else otherTest">
    

我也更新了您的stackblitz示例。我希望这会有所帮助。

docs here