limitTo:不在AngularJS中工作

Muh*_*iaz 4 javascript jquery loops limit angularjs

AngularJS

userData.success(function (userdataobject) {
                  $scope.catadata = userdataobject;
                $scope.quantity = 1;
            });
            userData.success(function (userdataobject) {
                  $scope.catadata1 = userdataobject;
                $scope.quantity1 = 1;
            });
Run Code Online (Sandbox Code Playgroud)

AngularJS 2不同的循环代码

环1

 <div ng-repeat="cat in catadata | limitTo:quantity | filter: {TopCategoryID : 11}">
Run Code Online (Sandbox Code Playgroud)

回路2

<div ng-repeat="cat1 in catadata1 | limitTo:quantity1 | filter: {TopCategoryID : 12}">
Run Code Online (Sandbox Code Playgroud)

limitTo:quantity 正在工作,但limitTo:quantity1没有工作

and*_*lrc 5

TLDR:你必须将limitTo过滤器移到最后:

<div ng-repeat="cat1 in catadata1 | filter: {TopCategoryID : 12} | limitTo:quantity1">
Run Code Online (Sandbox Code Playgroud)

limitTo过滤器会创建具有所需长度的新数组,那么您的过滤器将被应用,可以这样考虑:

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}));
Run Code Online (Sandbox Code Playgroud)

另一种观察方式是:

var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [] // filter: 2
Run Code Online (Sandbox Code Playgroud)

而:

var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [0] // filter: 0
Run Code Online (Sandbox Code Playgroud)

这是您的代码中发生的事情,只是使用另一个过滤器.