как считать элементы во вложенном ng-repeat без $ index - PullRequest
1 голос
/ 18 июня 2019

Я хочу посчитать итерацию ng-repeat, когда условие совпадает.

Я пробовал $index, но он печатает для всех итераций / элементов во вложенном ng-repeat

Fiddleссылка: https://jsfiddle.net/gdr7p1zj/1/

<tbody ng-controller="MainCtrl">
    <tr ng-repeat-start="a in test1">
          <td>{{a.categoryName}}(count_here)</td>
        </tr>
        <tr ng-repeat-end ng-repeat="b in test" ng-if="a.categoryId==b.categoryId">
          <td>{{b.name}}</td>
        </tr>
</tbody>
i want like this 
category_one(4)  <=item count 4 items in this category so 4 will display
    item1
    item2
    item3
    item4 
category_two(2)
    item5
    item6
<!-- this is in controller -->

$scope.test1=[{
        categoryId:'1',categoryName:'category one'
    },
    {
        categoryId:'2',categoryName:'category two'
    }]
    $scope.test = [
        {categoryId:'1',name:'cate 1 elem0'},
        {categoryId:'1',name:'cate 1 elem1'},
        {categoryId:'2',name:'cate 2 elem'}
    ];
});      

Ответы [ 2 ]

0 голосов
/ 20 июня 2019

Спасибо за вашу помощь. Но я получаю ожидаемый вывод без каких-либо вызовов функций или фильтров

Здесь скрипка Ссылка: https://jsfiddle.net/wk3nzj96/

htmlCode:

<div ng-app='myapp' >
<div ng-controller="MainCtrl">
  <table ng-init="$scope.counter=0">

    <tr ng-repeat-start="cate in mainCategory">
     <td>   {{cate.name}} ({{$scope.counter[$index]}})</td></tr>

    <tr ng-repeat="itemsItr in items" ng-init="$scope.counter[$parent.$parent.$index]=$scope.counter[$parent.$parent.$index]+1" ng-if="itemsItr.mid==cate.id">
        <td>{{itemsItr.name}}</td>
    </tr>
    <tr ng-repeat-end ng-if="false"></tr>
  </table>

</div>
</div>

и ControllerCode:

(function() {
  angular.module('myapp', []).controller('MainCtrl', function($scope) {
     $scope.mainCategory = [
   { name: "categoryOne",id:1 },
   { name: "categoryTwo",id:2 }
    ];
     $scope.items = [
   { name: "item1FromCateOne" ,mid:1 },
   { name: "item2FromCateOne",mid:1 },
   { name: "item3FromCateOne" ,mid:1 },
   { name: "item1FromCateTwo",mid:2 }
    ];

  });

Это стандартный способ сделать это?

0 голосов
/ 19 июня 2019

Можно создать функцию (getCount) в контроллере, которая будет считать, что-то вроде этого:

$scope.getCount = function(categoryId) {        // returns the count by matching by categoryId
  return $scope.test.filter(function(element) { // first, filter elements in `test`
    return element.categoryId === categoryId;   // matching by categoryId
  }).length;                                    // then, return the count of how many results we got after the filter
}

И в html-вызове эта функция будет выглядеть так:

<tbody ng-controller="MainCtrl">
    <tr ng-repeat-start="a in test1">
      <td>{{a.categoryName }} ({{getCount(a.categoryId)}})</td> <!-- <-- call the function in order to display the count -->
    </tr>
    <tr ng-repeat-end ng-repeat="b in test" ng-if="a.categoryId == b.categoryId">
      <td>{{b.name}}</td>
    </tr>
</tbody>

Смотрите демонстрацию здесь: https://jsfiddle.net/lealceldeiro/v9gj1ok4/11/

...