角度ngIf条件ngFor用于显示类似按钮

时间:2019-05-10 05:41:42

标签: node.js angular ngfor

我正在使用有角按钮,要实现类似按钮取决于ngFor循环内的ngif条件,并且我从两个MYSQL表帖子(post_id,user_id,description),点赞(like_id,user_id,post_id,like_status)中获取数据)。我基于user_id加入了两个表,而我只是ngIf实现,条件取决于user_id&post_id&like_status。

我写了这三个条件来显示按钮

* 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 =='不喜欢')) “

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

但是前两个条件可以正常工作,但如果user_id和post_id不在同一行中,则最后一个条件不起作用,因此任何一个请更正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>


  </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);
      }
    );
  }

一旦检查我的链接即可在stackBlitz中进行实时代码编辑

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

1 个答案:

答案 0 :(得分:2)

它不起作用,因为*ngIf="(postLikes.length == 0) ...永远不会在您的<div *ngFor="let like of postLikes; let j = index ">内部为真

如果postLikes.length == 0,则*ngFor不会完成模板的任何迭代。

实际上,这里实际上不需要所有postLikes.length逻辑。