简体   繁体   English

limitTo:不在AngularJS中工作

[英]limitTo: not working in AngularJS

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 Loops code AngularJS 2不同的循环代码

Loop-1 环1

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

Loop-2 回路2

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

limitTo:quantity is working but limitTo:quantity1 is not working limitTo:quantity正在运行但是limitTo:quantity1不起作用

TLDR: You will have to move the limitTo filter to the end: TLDR:您必须将limitTo过滤器移动到结尾:

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

The limitTo filter will create a new array with the desired length, then your filter will be applied, consider this: 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}));

Another way to look at is: 另一种观察方式是:

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 is happening in your code, just with another filter. 这是您的代码中发生的事情,只是使用另一个过滤器。

Probably because you have referenced the same array and the first loop is executed first. 可能是因为您引用了相同的数组并且首先执行了第一个循环。 Try to copy the array before assigning it to the scope. 尝试在将数组分配给范围之前复制该数组。 Example: 例:

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

If this does not solve your problem please create a Plunker in which you will reproduce the scenario. 如果这不能解决您的问题,请创建一个Plunker,您将在其中重现该场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM