简体   繁体   English

使用ngIf else角度比较两个数组值

[英]angular comparing two array values using ngIf else to show button

I am working on two arrays with ngIf else condition , I got two array values from two tables posts[post_id, user_id, description], likes[post_id, user_id, like_status, like_id].Here i need to compare post_id, user_id and like_status='like' if they are equal then condition true show success button else false default button. 我正在使用ngIf else条件处理两个数组,我从两个表post [post_id,user_id,description],likes [post_id,user_id,like_status,like_id]中获得了两个数组值。在这里,我需要比较post_id,user_id和like_status =如果它们相等,则“喜欢”,则条件为true显示成功按钮,否则为false默认按钮。

I am already tried but its not working so please help to compare two arrays in using ngIf else . 我已经尝试过了,但是无法正常工作,因此请在使用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;





Please try my StackBlitz code once and correct the error 请尝试一次我的StackBlitz代码并更正错误

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

I have updated code, please find changes 我已更新代码,请查找更改

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

Hope this helps you. 希望这对您有所帮助。

There is a problem with postlikes condition in html you compare string with array Ngfor for postlikes array is remaining html中的postlikes条件存在问题,您将字符串与数组Ngfor进行比较,以保留postlikes数组

I recommend you to do this in typescript using find keyword to compare and pass that result on posts objects like boolean from that it shows and hide your button 我建议您在typescript中使用find关键字进行此操作,以比较结果并将结果传递给boolean这样的发布对象,并显示并隐藏您的按钮

One way to do this 一种方法

  1. Create a public method in your component as 在您的组件中创建一个公共方法为

     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. Now call this method in your view as 现在在您的视图中将此方法称为

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

I have updated you stackblitz example as well. 我也更新了您stackblitz示例。 I hope this helps. 我希望这有帮助。

https://stackblitz.com/edit/angular-wddupe?embed=1&file=src/app/app.component.html https://stackblitz.com/edit/angular-wddupe?embed=1&file=src/app/app.component.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM