简体   繁体   English

使用 ngFor 和 ngIf 在数组中迭代两种不同的方式 - Angular

[英]Iterate two different ways in an array with ngFor and ngIf - Angular

I am trying to get this array (which will be at the end coming from an API to generate real components):我正在尝试获取此数组(最终将来自 API 以生成实际组件):

salesArray = [0, 1, 2, 3, 4, 5, 6, 7];

to allow me to generate a component several times in Bootstrap rows and cols.允许我在 Bootstrap 行和列中多次生成组件。

The idea is to offer two different views the user can switch with a button.这个想法是提供两种不同的视图,用户可以通过一个按钮来切换。 This part is not directly linked to the problem.这部分与问题没有直接联系。

First view = a Bootstrap row with 3 col-4, so the result should be:第一个视图 = 一个带有 3 col-4 的 Bootstrap 行,所以结果应该是:

0 - 1 - 2 0 - 1 - 2

3 - 4 - 5 3 - 4 - 5

6 - 7 - empty col 6 - 7 - 空列

Second view = a Bootstrap row with 2 col-4 (not col-6 because the element which will be further called from the API has to stay the same size), so the result should be:第二个视图 = 带有 2 col-4 的 Bootstrap 行(不是 col-6,因为将从 API 进一步调用的元素必须保持相同的大小),所以结果应该是:

0 - 1 0 - 1

2 - 3 2 - 3

4 - 5 4 - 5

6 - 7 6 - 7

Here is my code for now:这是我现在的代码:

<div class="container-fluid">
  <div class="row">
  <div class="col-9">
        <ng-container *ngIf="screenResolution == 'Desktop'">
            <ng-container *ngFor="let item of salesArray; let i = index;">
                <div class="row" *ngIf="i%3 === 0">
                    <ng-container *ngFor="let item of salesArray; let i = index;">
                    <div class="col-4">
                        {{ i }}
                        <!-- <srp-sale-block [dataView]=dataToDisplay></srp-sale-block> -->
                    </div>
                </ng-container>
                </div>
            </ng-container>
        </ng-container>
  </div>
  </div>
</div>

For now, I manage to generate a row every three components, but after this the loop iterate again through all the array, and I don't get how to show only 2 or 3 numbers by row, and then keep going with the next series.现在,我设法每三个组件生成一行,但在此之后循环再次遍历所有数组,我不知道如何按行仅显示 2 或 3 个数字,然后继续下一个系列. So 0 - 1 - 2 in the first row, then 3 - 4 - 5 in the second...所以第一行是 0 - 1 - 2,第二行是 3 - 4 - 5 ...

Thanks.谢谢。

You can probably get away with just using [ngClass] directive to apply a bootstrap column class based on the resolution.您可能可以仅使用[ngClass]指令根据分辨率应用引导列类。 If the resolution is Desktop apply class col-4 to give you 3 columns , else apply class col-6 to give you 2 columns.如果分辨率是桌面,则应用类col-4为您提供 3 列,否则应用类col-6为您提供 2 列。

sales.component.html销售.component.html

<div class="row">
  <ng-container *ngFor="let item of salesArray">
    <div [ngClass]="
        { 'col-4' : resolution === 'Desktop', 'col-6' : resolution === 'Mobile' }">
      {{item}}
    </div>
  </ng-container>
</div>

Not quite sure if this is what you're looking for.不太确定这是否是您要查找的内容。 Here is a stackblitz example...这是一个stackblitz示例......

https://stackblitz.com/edit/angular-bootstrap-4-starter-jsiaw1?file=src/app/sales/sales.component.html https://stackblitz.com/edit/angular-bootstrap-4-starter-jsiaw1?file=src/app/sales/sales.component.html

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

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