[英]AngularJS, problems with $index variable in ng-repeat
I'm trying to fill out a 2 column table in AngularJS.我正在尝试在 AngularJS 中填写一个 2 列的表格。 I'm using ng-repeat
directive to fill out the table, but it's not's working the way I'm planning.我正在使用ng-repeat
指令来填写表格,但它并没有按照我计划的方式工作。 My $scope.items
is: [Coors, Jameson, Bacardi, Corona]
I want the table to look like this:我的$scope.items
是: [Coors, Jameson, Bacardi, Corona]
我希望桌子看起来像这样:
however, it looks like this:但是,它看起来像这样:
I'm confused as to why the [$index+1]
directive in the my script is only working in the actual text portion of the script (in parenthesis), while the <item-card>
div does not seem to properly displaying items[$index+1]
, and instead is displaying items[$index]
.我很困惑为什么我的脚本中的[$index+1]
指令仅在脚本的实际文本部分(括号中)有效,而<item-card>
div 似乎没有正确显示items[$index+1]
,而是显示items[$index]
。 Here is my script:这是我的脚本:
<div class=row ng-repeat="item in items" ng-if="$index %2 ==0">
<div class="col col-50" ng-if="$index < items.length">
<item-card item="{{item[$index]}}"></item-card>
({{$index}})
</div>
<div class="col col-50" ng-if="$index +1 < items.length">
<item-card item="{{items[$index+1]}}"></item-card>
({{$index+1}})
</div>
</div>
Does anyone know why this might not be working as intended?有谁知道为什么这可能无法按预期工作?
Edit: Including is itemcard.html .编辑:包括itemcard.html 。
<div class = "card" >
<img id = "cardImage" ng-src= "data:image/jpeg;base64,{{item.image}}" width = "100%"/>
{{item.cartQuantity}}
<cardHeader>{{item.itemName}}</cardHeader><br>
<cardHeader ng-if= "item.paksize >1">{{item.paksize}} pack</cardHeader>
<button class="button" ng-click="addToCart(item)">+</button>
<button class="button" ng-click="decrementCart(item)">-</button>
</div>
What you're trying to do seems a little odd to me.你试图做的事情对我来说似乎有点奇怪。 Rather than trying to split the array of items
into even chunks of 2 columns with Directive Fu, might you consider using lodash.chunk
?您可以考虑使用lodash.chunk
,而不是尝试使用 Directive Fu 将items
数组拆分为 2 列的偶数块吗?
<script>
angular.module('example', [ ])
.run(function($scope){
$scope.items = _.chunk([ /* . . . */ ], 2);
});
</script>
<div ng-app="example">
<div class=row ng-repeat="chunk in items">
<div class="col col-50">
<item-card item="{{chunk[0]}}"></item-card>
({{$index}})
</div>
<div class="col col-50">
<item-card item="{{chunk[1]}}"></item-card>
({{$index+1}})
</div>
</div>
</div>
If you wanted to be really Angular about it (a pun!), you could register lodash.chunk
as a custom Filter instead:如果您想真正了解它(双关语!),您可以将lodash.chunk
注册为自定义过滤器:
<script>
angular.module('example', [ ])
.run(function($scope){
$scope.items = [ /* . . . */ ];
})
.filter('chunk', function(){
return _.chunk;
});
</script>
<div ng-app="example">
<div class=row ng-repeat="chunk in items | chunk:2">
<div class="col col-50">
<item-card item="{{chunk[0]}}"></item-card>
({{$index}})
</div>
<div class="col col-50">
<item-card item="{{chunk[1]}}"></item-card>
({{$index+1}})
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.