AngularJS
userData.success(function (userdataobject) {
$scope.catadata = userdataobject;
$scope.quantity = 1;
});
userData.success(function (userdataobject) {
$scope.catadata1 = userdataobject;
$scope.quantity1 = 1;
});
AngularJS 2 种不同的循环代码
循环 1
<div ng-repeat="cat in catadata | limitTo:quantity | filter: {TopCategoryID : 11}">
循环 2
<div ng-repeat="cat1 in catadata1 | limitTo:quantity1 | filter: {TopCategoryID : 12}">
limitTo:quantity
有效,但 limitTo:quantity1
无效
最佳答案
TLDR:您必须将 limitTo
过滤器移至末尾:
<div ng-repeat="cat1 in catadata1 | filter: {TopCategoryID : 12} | limitTo:quantity1">
limitTo
filter将创建一个具有所需长度的新数组,然后将应用您的过滤器,考虑一下:
var catadata1 = [
{TopCategoryID: 13, ...},
{TopCategoryID: 42, ...},
{TopCategoryID: 12, ...},
...
];
// When we apply the limitTo, this is what's happening:
var limitTo = catadata1.slice(0, 1); // Keep the first item
// And when we apply the filter we only filter on the limitTo array:
var filter = limitTo.filter(matchItem({TopCategoryID : 12}));
另一种查看方式是:
var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [] // filter: 2
同时:
var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [0] // filter: 0
这就是您的代码中发生的情况,只是使用了另一个过滤器。
关于javascript - limitTo : not working in AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665080/