[英]Angularjs limitTo not working when use (key,value)
I want to make a vertical menu from array like this: 我想从这样的数组制作垂直菜单:
{"sport":"Soccer","country":"Brazil","Teams":"TeamA vs Teamb"}
So I need group it by sport
, next by country
, then when I make a click at Soccer button, I can see all Country with this sport. 因此,我需要按
sport
分组,然后按country
分组,然后单击“足球”按钮,就可以看到所有具有该运动的国家/地区。 But this array is so big and I need limitTo
filter for max 10 country per Sport and when 'more button' is clicked I can increase the max in 10 per each click. 但是这个数组太大了,我需要使用
limitTo
筛选每个Sport最多10个国家/地区,当单击“更多按钮”时,我可以将每次单击的最大值增加10个。 I have an example in 我有一个例子
But the limitTo
is not limiting as expected. 但是
limitTo
并不像预期的limitTo
受到限制。 How can I fix it? 我该如何解决?
Update the solution now is with value.slice(stard,end)
I looking a method with limitTo, is because the array is unsorted, and no always have the required result. 现在使用
value.slice(stard,end)
更新解决方案。我正在寻找一个带有limitTo的方法,这是因为数组未排序,并且始终没有所需的结果。
The problem is that limitTo
filter needs an array to work. 问题是
limitTo
过滤器需要一个数组才能工作。
You could use toArray
filter from this github repo . 您可以使用此github存储库中的
toArray
过滤器。
The $filter
calls in the controller could be moved back to markup that was just a test during finding the issue. 控制器中的
$filter
调用可以移回标记,这只是在发现问题期间进行的测试。
Below is the updated code with the toArray
filter or in this jsfiddle . 以下是使用
toArray
过滤器或在此jsfiddle中更新的代码。
var myApp = angular.module("myApp",['angular.filter', 'angular-toArrayFilter']); myApp.controller('MyCtrl', function MyCtrl($scope, $filter) { $scope.list = [{"sport":"Soccer","country":"Brazil","Teams":"TeamA vs Teamb"}, {"sport":"Baseball","country":"USA","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"USA","Teams":"TeamA vs Teamb"}, {"sport":"Baseball","country":"USA","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"Cuba","Teams":"TeamA vs Teamb"}, {"sport":"Baseball","country":"Brazil","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"Germany","Teams":"TeamA vs Teamb"}, {"sport":"Baseball","country":"Russia","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"USA","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"Spain","Teams":"TeamA vs Teamb"}, {"sport":"Soccer","country":"Brazil","Teams":"TeamA vs Teamb"} ]; console.log($scope.list); $scope.sports = $filter('groupBy')($scope.list, 'sport'); $scope.countries = $filter('groupBy')($scope.list, 'country'); });
.country{ margin-left:50px; }
<script src="https://code.angularjs.org/1.4.0/angular.js"></script> <script src="https://cdn.rawgit.com/petebacondarwin/angular-toArrayFilter/master/toArrayFilter.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.5/angular-filter.js"></script> <div ng-controller="MyCtrl" ng-app="myApp"> <div ng-repeat="(key,value) in sports"> {{key}} <div ng-repeat="countryObj in countries | toArray | limitTo:3"> <span class="country">{{countryObj[0].country}}</div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.