javascript - 显示超出其范围的 ng-repeat 项目数

标签 javascript angularjs

我正在重复这样的 JSON 对象(嵌套)项目列表:

 <div ng-repeat='item in items'>
  <ul>
     <li ng-repeat='specific in item'>{{specific}}</li>
  </ul>
</div> 

我想在页面顶部的每个项目对象中显示“特定”项目的数量(在 ng-repeat 范围之外)。我无法使用 items.length,因为我想显示特定项目的数量(必须如此循环),但我不知道如何绑定(bind) ng-repeat 中的数据并在其范围之外使用它..

最佳答案

 <span>Number of items :  {{countItems(items.length)}}</span>

$scope.countItems = function(items) {
     var count = 0;
     for(var item in items) {
         for(var specific in item) {
             count++;
         }
         // Instead of the second for, you can just write : 
         // count = count + item.length;
     }
     return count;
};

应该可以解决问题:)

关于javascript - 显示超出其范围的 ng-repeat 项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35476273/

相关文章:

javascript - 在 IE Mobile 中捕获 javascript 事件

javascript - 滚动到水平溢出的元素

javascript - 从字符串中提取域

javascript - 谷歌浏览器 : can't get current tab's url

javascript - (Scirra) 对数组的 AJAX 请求

javascript - 如何获取正在运行的函数所属的对象名?

javascript - 当我将数据放在 "distribuidora"字段时,我的表没有更新

javascript - Chrome插件CORS Toggle无法使用POST/PUT/DELETE方法工作

css - 升级 Angular Material 打破了 md-input 换行符

javascript - 设置 Highcharts 系列中使用的数据格式