【问题标题】:Looping angular object in bootstrap grid在引导网格中循环角度对象
【发布时间】:2020-08-02 08:51:01
【问题描述】:
<div class="container">
  <div class="row">
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
  </div>
</div>

我只想在我的 Angular 应用程序中将卡片显示为特定的网格

连续 3 张牌,然后在下一行

   <div class="container" *ngFor="let item of post">
      <div class="row">
        <div class="col-sm">
          <h2>{{item.title}}</h2>
    
        </div>
        
      </div>
    </div>

结果是这样的

而不是列

它只是将那些对象数据作为行获取 有什么方法可以让我们遍历该网格并相应地显示数据

Note:I have tried Angular material Grid 

我尝试过使用 Flexlayout 仍然没有成功

我只需要一种可能的方式来显示这样的数据

【问题讨论】:

    标签: angular typescript twitter-bootstrap angular-material angular-bootstrap


    【解决方案1】:

    您面临的问题是您正在容器 div 上创建loop;然后每张卡创建 3 个容器而不是单个容器。

       <div class="container" *ngFor="let item of post">
          <div class="row">
            <div class="col-sm">
              <h2>{{item.title}}</h2>
        
            </div>
            
          </div>
        </div>
    

    为了实现您想要的(带有N 卡片的容器),您必须在卡片元素上声明loop

       <div class="container">
          <div class="row">
            <div class="col-sm" *ngFor="let item of post">
              <h2>{{item.title}}</h2
            </div>
          </div>
        </div>
    

    这样您将拥有一个容器,然后是一行,最后是从迭代中创建的组件列表。

    你可以在这里读一点: Angular ngFor example 和这里 ngForOf Directive

    【讨论】:

    • 是的,这工作得非常好,但是如果我们想在 3 张卡片之后将卡片卡片添加到另一行怎么办,因为有了这种 UI 结构,卡片很可能会在单个列中被弄乱,我们可以确保只有 3 张卡片可以连续
    • 我建议将数组重构为长度为 3 的子数组的数组。然后,您将为每个子数组的 row 创建一个循环,然后为子数组的每个元素循环。这个答案可以提供一些启示:stackoverflow.com/questions/58399994/…
    【解决方案2】:
    <div class="container" >
          <div class="row">
            <div class="col-sm" *ngFor="let item of post">
              <h2>{{item.title}}</h2>
        
            </div>
            
          </div>
        </div>
    

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 2018-05-23
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多