You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS:当ng-repeat循环为空时如何添加0?

嘿,这个需求我太熟啦!在AngularJS里处理ng-repeat循环为空时显示0的情况,分两种常见场景,对应不同的实现方式,我给你一步步说清楚:

1. 针对整个dayInterval循环为空的情况

如果是当dayInterval数组完全没有数据时,要显示一行带0的内容,你可以在原有循环行的后面,加一个判断为空的兜底行:

<!-- 你原有的循环行 -->
<tr ng-repeat="di in dayInterval track by di.id" ng-class="{weekend: di.dayName=='Samedi'|| di.dayName=='Dimanche'}">
    <!-- 这里保留你原来的单元格内容,比如嵌套的td、label等 -->
</tr>
<!-- 空状态兜底行 -->
<tr ng-if="dayInterval.length === 0">
    <!-- 注意:colspan的值要和你表格的总列数一致,确保0能占满整行,避免布局错乱 -->
    <td colspan="6">0</td>
</tr>

2. 针对单元格内部子循环为空的情况

看你代码里还有嵌套的ptt in value.ptasks循环,如果是这个子循环没有匹配项时要显示0,可以用过滤器配合ng-if来判断:

<td ng-repeat="pr in profiles track by pr.i.id_profile" ng-if="gr.id_groupe==pr.i.groupe" style="min-width:150px !important; max-width:150px !important; height:40px !important;font-size:12px;">
    <!-- 你原有的任务标签渲染 -->
    <label ng-repeat="ptt in value.ptasks track by ptt.id_profile" ng-if="ptt.id_profile==pr.i.id_profile">
        {{ptt.content}} <!-- 替换成你原本要显示的任务内容 -->
    </label>
    <!-- 子循环无匹配项时显示0 -->
    <span ng-if="(value.ptasks | filter: {id_profile: pr.i.id_profile}).length === 0">0</span>
</td>

这里用filter过滤器筛选出和当前pr.i.id_profile匹配的任务项,判断筛选后的结果长度为0时,就显示0。

内容的提问来源于stack exchange,提问作者DuliNini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:19:58