LimitTo: not working in AngularJS

Angularjs

userData.success(function (userdataobject) {
                  $scope.catadata = userdataobject;
                $scope.quantity = 1;
            });
            userData.success(function (userdataobject) {
                  $scope.catadata1 = userdataobject;
                $scope.quantity1 = 1;
            });

AngularJS 2 different loop code

Loop 1

 <div ng-repeat="cat in catadata | limitTo:quantity | filter: {TopCategoryID : 11}">

Loop 2

<div ng-repeat="cat1 in catadata1 | limitTo:quantity1 | filter: {TopCategoryID : 12}">

limitTo:quantityworks but limitTo:quantity1doesn't work

+4
source share
2 answers

TL; DR: You will need to move the filter limitToto the end:

<div ng-repeat="cat1 in catadata1 | filter: {TopCategoryID : 12} | limitTo:quantity1">

limitTofilter will create a new array with the desired length, then your filter will be applied, consider the following:

var catadata1 = [
    {TopCategoryID: 13, ...},
    {TopCategoryID: 42, ...},
    {TopCategoryID: 12, ...},
    ...
];
// When we apply the limitTo, this is what 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}));

Another way to look:

var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [] // filter: 2

While:

var a = [0, 1, 2]; // Initial Array
var b = [0] // limitTo: 1
var c = [0] // filter: 0

This is what happens in your code, only with a different filter.

+5
source

, , , . , . :

userData.success(function (userdataobject) {
    $scope.catadata = angular.copy(userdataobject);
    $scope.quantity = 1;
});
userData.success(function (userdataobject) {
    $scope.catadata1 = angular.copy(userdataobject);
    $scope.quantity1 = 1;
});

, Plunker, .

0

Source: https://habr.com/ru/post/1608102/


All Articles