【问题标题】:angular ngIf condition inside ngFor for to display like buttonngFor中的角度ngIf条件用于显示类似按钮
【发布时间】:2019-09-27 23:51:48
【问题描述】:

我正在使用角度按钮,要实现类似按钮取决于 ngFor 循环内的 ngif 条件,我从两个 MYSQL 表中获取数据)。我根据 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 == 'unlike')) "

*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

【问题讨论】:

  • 最后两个条件是互斥的,你的 stackblitz 不包括你的“第三个”按钮应该可见的情况。
  • 如果你知道请更正我的 stackBlitz 代码,第三个按钮应该在第 6、7、8 个帖子中以默认颜色可见
  • 同样,它要求 postLikes 为 0。那就是 5。
  • 你不能创建一个普通的数据模型,在这个模型中帖子有喜欢作为属性,而不是把它分成数组来做任何反应吗?
  • 我正在尝试创建 youtube 克隆喜欢不喜欢的按钮,每个按钮都有三个阶段,默认,喜欢不喜欢,在默认状态下,表中没有特定 user_id 和 post_id 的数据,第二状态如果他点击喜欢按钮然后like_status =“like”,在第三状态如果他点击不喜欢然后like_status =“不喜欢”,我只使用两个按钮,没有第三个按钮,每个按钮有三个状态,默认,喜欢,不一样。

标签: node.js angular ngfor


【解决方案1】:

它不起作用,因为 *ngIf="(postLikes.length == 0) ... 永远不会在您的 &lt;div *ngFor="let like of postLikes; let j = index "&gt; 中评估为 true

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

事实上,这里所有的postLikes.length逻辑其实都是不必要的。

【讨论】:

  • 那么,postLikes.length == 0 可以在for 循环之后使用吗?
  • @KingStone 是的!您可以随时检查,因为该属性来自您的component.ts
  • 能否请您给我建议,以根据我的要求获得确切的输出,如果同一行中没有 user_id 和 post_id,则在第 6、7、8 个帖子中显示按钮。
猜你喜欢
  • 2019-09-26
  • 1970-01-01
  • 2019-07-09
  • 2023-03-04
  • 2019-08-22
  • 2021-01-07
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
相关资源
最近更新 更多