具有阵列长度条件的角度ng重复

时间:2022-08-23 23:45:26

I want the ng-repeat to run only when the photos.length > 1

我希望ng-repeat只在photos.length> 1时运行

Doing this doesn't seem to work:

这样做似乎不起作用:

<div ng-repeat="thumb in photos | photos.length > 1">

1”>

Is there any quick trick / elegant way to do this? I just need to check for array length and nothing else fancy.

有没有快速的技巧/优雅的方式来做到这一点?我只需要检查数组长度,没有别的想象。

I would hate to duplicate the array to another array by doing something like this

我不想通过做这样的事情将数组复制到另一个数组

if (oldArray.length > 1)
 newArray = photos;
else
 newArray = [];

...and then do ng-repeat on newArray. This seems inefficient.

...然后在newArray上执行ng-repeat。这似乎效率低下。

Thanks.

谢谢。

2 个解决方案

#1


39  

Try:

尝试:

<div ng-repeat="thumb in photos" ng-if="photos.length > 1"></div>

if the ng-if condition is not satisfied those elements will be removed from the DOM. See http://docs.angularjs.org/api/ng/directive/ngIf for further reference.

如果不满足ng-if条件,那么这些元素将从DOM中删除。有关进一步的参考,请参见http://docs.angularjs.org/api/ng/directive/ngIf。

#2


5  

You could try using the ng -if directive to check the condition you describe e.g.

您可以尝试使用ng -if指令来检查您描述的条件,例如

<div ng-if='photos.length > 1'> your HTML here </div>

#1


39  

Try:

尝试:

<div ng-repeat="thumb in photos" ng-if="photos.length > 1"></div>

if the ng-if condition is not satisfied those elements will be removed from the DOM. See http://docs.angularjs.org/api/ng/directive/ngIf for further reference.

如果不满足ng-if条件,那么这些元素将从DOM中删除。有关进一步的参考,请参见http://docs.angularjs.org/api/ng/directive/ngIf。

#2


5  

You could try using the ng -if directive to check the condition you describe e.g.

您可以尝试使用ng -if指令来检查您描述的条件,例如

<div ng-if='photos.length > 1'> your HTML here </div>