angular - ngFor 基于类型
问题描述
现在它打印两次没有数据。我只想替换没有数据的第三个,因为数组中不存在类型 3。
所以我的条件是有时我得到类型为 3 或类型 2 或类型 1 的数组,如果数组没有类型 3 或类型 2 或类型 1,那么只打印一次。
其实我没有办法做:(
堆栈闪电战:https ://stackblitz.com/edit/angular-r85hhs?file=src%2Fapp%2Fapp.component.html
app.component.html
<ng-container *ngFor="let item of test">
<ng-container *ngIf="item.type === 1">
<p> {{item.name}}</p>
</ng-container>
<ng-container *ngIf="item.type === 2">
<p> {{item.name}}</p>
</ng-container>
<ng-container *ngIf="item.type === 3">
<p>{{item.name}}</p>
</ng-container>
<ng-container *ngIf="item.type !== 3">
NO data
</ng-container>
</ng-container>
app.component.ts
public test = [{type:1, name:'shawn'},
{type:2, name:"ronny"}
]
解决方案
更新到这个
<hello name="{{ name }}"></hello>
<p>Start editing to see some magic happen :)</p>
<hr />
<div *ngIf="test?.length > 0">
<ng-container *ngFor="let item of test">
<ng-container *ngIf="item.type === 1">
<p>{{ item.name }}</p>
</ng-container>
<ng-container *ngIf="item.type === 2">
<p>{{ item.name }}</p>
</ng-container>
<ng-container *ngIf="item.type === 3">
<p>{{ item.name }}</p>
</ng-container>
</ng-container>
</div>
<div>
<ng-container *ngIf="item?.length === 0">NO data</ng-container>
</div>
推荐阅读
- json - 使用 curl 将团队添加到存储库的正确方法
- batch-processing - 我们可以对批处理进行窗口化吗?
- clang-static-analyzer - 如何匹配指向空对象的指针?
- javascript - 使用 javaScript 循环对象数组的最佳方法是什么?
- python - 使用 == 符号时的 if 和语句混淆
- amazon-web-services - 如何解决 AWS 爬虫在双引号内拆分逗号并破坏数据目录的问题?
- node.js - 为什么我的表单数据在 Node.js/Express 中被截断?
- json - 如何通过 dio 编码并发送到服务器 json 列表
- volttron - Volttron 在尝试发布时抛出属性错误
- python - 'float' 对象没有属性 'append'