Angularjs 检查数组的大小

Angularjs 检查数组的大小,angularjs,angularjs-scope,ng-show,angular-ng-if,ng-hide,Angularjs,Angularjs Scope,Ng Show,Angular Ng If,Ng Hide,我有一个数组,总是返回20项。但是,如果返回的项目少于20个,则返回“”而不是减少数组中的数量 例如,myArray包含20项(仅15个值) 我想根据myArray中项目的填充数量对某些元素执行ng show/hide/if 最初我尝试: <div ng-show="myArray.length > 10"> Show if more than 10 items </div> <div ng-show="myArray.length > 15"

我有一个数组,总是返回20项。但是,如果返回的项目少于20个,则返回“”而不是减少数组中的数量

例如,
myArray
包含20项(仅15个值)

我想根据myArray中项目的填充数量对某些元素执行ng show/hide/if

最初我尝试:

<div ng-show="myArray.length > 10">
    Show if more than 10 items
</div>

<div ng-show="myArray.length > 15">
    Show if more than 15 items
</div>

<div ng-show="myArray.length > 19">
    Show if more than 19 items
</div>

显示是否超过10项
显示是否超过15项
显示是否超过19项
当然,以上所有内容都会显示出来,因为总会有20件物品被退回


我可以通过ng show/hide/if实现这一点,而不必在控制器中写入额外的JS吗?

只需过滤掉空白项:

var outBlanks = Boolean;

$scope.nonBlanksCount = $scope.myArray.filter(outBlanks).length;

<div ng-show="nonBlanksCount > 10">
    Show if more than 10 items
</div>

但是,在这种情况下,过滤器将执行多次,这不是很有效。

如果您只是像这样扩充阵列,它是否适用于您

$scope.myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, "", "", "", ""].filter(Boolean);

因为你偷了我的答案,所以我偷了你的一部分;)使用
Boolean
显然更优雅。当然,我只是想改进一下你的,因为他想在这个范围内写一点js,或者根本不写js,但是本机函数不能在这样的绑定中使用,所以他不得不这样做。所以,这几乎是我试图让你的答案变短的唯一答案,我喜欢你的第二个答案。。过滤板会是什么样子?
<div ng-show="(myArray | filterBlanks).length > 10">
    Show if more than 10 items
</div>
$scope.myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, "", "", "", ""].filter(Boolean);