arrays - Angular ngFor连续显示两个项目
问题描述
我的代码遇到了问题。我正在尝试使用 ngFor 命令显示 4 x 8 网格。下面提供的是我的代码:
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
<ng-container *ngFor="let word of Words; let i = index;">
<div class="row" *ngIf="((i % 2) == 0)" style="margin-right: 0px; margin-left: 0px;" >
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i+1 }}</div>
<div class="col-sm-5 text-center example-box" *ngIf="word == Words[i]" style="border: 1px solid black" cdkDrag>{{ word.Title }}</div>
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i+2 }}</div>
<div class="col-sm-5 text-center example-box" *ngIf="word == Words[i+1]" style="border: 1px solid black" cdkDrag>{{ word.Title }}</div>
</div>
</ng-container>
</div>
我遇到的问题是网格在左列中显示 word.Title,但在右列中它只显示数字 ({{ i + 2 }} ) 文本,现在是 word.Title 内容。见下图:
可以看到偶数列,新列的内容没有出现。
如何让 4 列出现在我的表格中?
先感谢您。
解决方案
我做了 3 处更改
- 在第一个子 div 上,我删除了 *ngIf 条件
- 在第二个子 div 上,我将 word.Title 更改为 Words[i]
- 在第三个子 div 上,我将 *ngIf 更新为 Words[i+1] !== undefined
- 在第四个子 div 上,我将 word.Title 更改为 Words[i+1]
推荐阅读
- git - 两个标签之间的Git汇总统计
- firebase - 我必须从 Firebase 下载图像才能显示它吗?
- sql - 这个 SQL 是做什么的?
- javascript - 如何在three.js中使用数组而不是MeshFaceMaterial?
- python - 如何设置要在 Flask-Cache 中缓存的 url 的命中数?
- cassandra - Cassandra Apache 查询
- angular - Angular 6:失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。(
- reactjs - Webpack 4,React 项目,Bundle 大小
- java - PlatformTransactionManager 如何在 Spring Transaction 内部工作?
- excel - 使用 VBA 和 Selenium 自动化 Google Chrome