简体   繁体   English

ngFor中两个增量,Angular5

[英]increment of two in ngFor , Angular5

I m having a piece of code in Angular 5 with ngFor as shown below 我在ngFor 5中使用ngFor编写了一段代码,如下所示

Angular Model 角度模型

 export class UserReviews {
  public PageName: string;
  public UserName: string;
  public Review: string;
  public Rating: number;
  public IsApproved: boolean;
}

Varibales Varibales

UserReviewModel: UserReviews = new UserReviews();
UserReviewList: UserReviews[] = [];
this.UserReviewList = [
      {
        PageName: "Page Name Here",UserName: "User1",Review:"Review Here",
        Rating: 3,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User2",Review:"Review Here",
        Rating: 4,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User3",Review:"Review Here",
        Rating: 3, IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User4",Review:"Review Here",
        Rating: 2,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User5",Review:"Review Here",
        Rating: 3,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User6",Review:"Review Here",
        Rating: 4,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User7",Review:"Review Here",
        Rating: 4,IsApproved: true
      },
      {
        PageName: "Page Name Here",UserName: "User8",Review:"Review Here",
        Rating: 5,IsApproved: true
      }
    ];

HTML Template HTML模板

<div class="row" *ngFor="let item of UserReviewList;let i = index">
    <div class="col-md-6 border-right-0">
      <div class="mb-1 text-grey">{{UserReviewList[i].UserName}}</div>
      <div class="mb-1">
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star-half-alt text-warning"></i>
        <strong class="ml-1">{{UserReviewList[i].Rating}}</strong>
      </div>
      <div class="mb-1">
        {{UserReviewList[i].Review}}
      </div>
    </div>
    <div class="col-md-6">
      <div class="mb-1 text-grey">{{UserReviewList[i+1].UserName}}</div>
      <div class="mb-1">
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star text-warning"></i>
        <i class="fas fa-star-half-alt text-warning"></i>
        <strong class="ml-1">{{UserReviewList[i].Rating}}</strong>
      </div>
      <div class="mb-1">
          {{UserReviewList[i].Review}}
      </div>
    </div>
  </div>

What i am trying to achieve here is to display reviews in row but in one row there should be two review Example : row 1 should have review 1 and 2, same row 2 have review 3 and 4 我在这里想要实现的是在行中显示评论,但在一行中应该有两个评论示例:第1行应具有评论1和2,同一行2应具有评论3和4

But problem is that it is showing review 1 and 2 in row 1 and row 2 it is showing review 2 and 3 instead on 3 and 4 so 2(even index item) is getting repeat again 但是问题在于它在第1行和第2行显示了评论1和2,在3和4上却显示了评论2和3,所以2(甚至索引项)再次重复

You can have an idea of this from below image 您可以从下面的图片中对此有所了解

在此处输入图片说明

AS shown here in second here , it should start with user 3 instead of user 4 and so on in 3rd row user 5 and user 6 如此处第二点所示,它应该以用户3而不是用户4开头,以此类推,在第三行中是用户5和用户6

My idea to achieve this is to increment i by 2 in ngFor 我的想法是在ngFor中将i加2

<div class="row" *ngFor="let item of UserReviewList;let i = index">

In let i =index ; 让我=索引; i++=2 我++ = 2

But seems like it is not allowed in angular , 但似乎不允许以尖角形式出现,

How can i achieve this in angular and also in angularjs ng repeat ? 我怎样才能做到有角度的并且也有角度重复

实现解决方案的更好方法是编写样式,使内容可以动态地分配给所需的位置,而不要跳过ngFor中的索引。

You can achieve this by adding only 您可以通过仅添加

<div class="row">
      <div class="col-md-6 border-right-0" *ngFor="let item of UserReviewList;let i = index">
            <div class="mb-1 text-grey">{{UserReviewList[i].UserName}}</div>
            <div class="mb-1">
                   <i class="fas fa-star text-warning"></i>
                   <i class="fas fa-star text-warning"></i>
                   <i class="fas fa-star text-warning"></i>
                   <i class="fas fa-star text-warning"></i>
                   <i class="fas fa-star-half-alt text-warning"></i>
                   <strong class="ml-1">{{UserReviewList[i].Rating}}</strong>
            </div>
            <div class="mb-1">
                  {{UserReviewList[i].Review}}
            </div>
       </div>

I think no need to add second div 我认为无需添加第二个div

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

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