Javascript 如何使用模运算删除ng repeat生成的未定义复选框?

Javascript 如何使用模运算删除ng repeat生成的未定义复选框?,javascript,angularjs,checkbox,Javascript,Angularjs,Checkbox,我有以下代码(见下文),它从给定列表创建多个复选框,通过使用ng=“$index%3==0”,我得到3列复选框 <div ng-controller="TestController" class="container"> <div ng-repeat="item in items" ng-if="$index % 3 == 0" class="row"> <div class="col-xs-4"> <in

我有以下代码(见下文),它从给定列表创建多个复选框,通过使用
ng=“$index%3==0”
,我得到3列复选框

<div ng-controller="TestController" class="container">
    <div ng-repeat="item in items" ng-if="$index % 3 == 0" class="row">
        <div class="col-xs-4">
            <input type="checkbox" ng-model="items[$index].id">&nbsp;
            <span>{{items[$index].name}}</span>
        </div>
        <div class="col-xs-4">
            <input type="checkbox" ng-model="items[$index+1].id">&nbsp;
            <span>{{items[$index+1].name}}</span>
        </div>
        <div class="col-xs-4">
            <input type="checkbox" ng-model="items[$index+2].id">&nbsp;
            <span>{{items[$index+2].name}}</span>
        </div>
    </div>
</div>

var app = angular.module('app', [ ]);
app.controller('TestController', ['$scope', function($scope) {

     $scope.items = [
    {id:0, name:"1/4 Mile"},
    {id:1, name:"1/2 Mile"},
    {id:2, name:"1 Mile"},
    {id:3, name:"2 Mile"},
    {id:4, name:"3 Mile"},
    {id:5, name:"4 Mile"},
    {id:6, name:"5 Mile"}
  ];
}]);

{{items[$index].name}
{{items[$index+1].name}
{{items[$index+2].name}
var-app=angular.module('app',[]);
app.controller('TestController',['$scope',函数($scope){
$scope.items=[
{id:0,名称:“1/4英里”},
{id:1,名称:“1/2英里”},
{id:2,名字:“1英里”},
{id:3,名字:“2英里”},
{id:4,名字:“3英里”},
{id:5,名字:“4英里”},
{id:6,名字:“5英里”}
];
}]);


问题是,如果列表中的项目数为奇数,我会得到额外的空白/未定义复选框。如何避免这种情况?

如果出现
情况,可以使用
ng。最后两个空白复选框是由于
项[$index+1]
项[$index+2]
引起的。由于您有
$index%3==0
并不意味着
$index+1
$index+2
存在,因此您可以使用
ng if
条件来检查
项数组中是否存在该值

var-app=angular.module('app',[]);
app.controller('TestController',['$scope',函数($scope){
$scope.items=[
{id:0,名称:“1/4英里”},
{id:1,名称:“1/2英里”},
{id:2,名字:“1英里”},
{id:3,名字:“2英里”},
{id:4,名字:“3英里”},
{id:5,名字:“4英里”},
{id:6,名字:“5英里”}
];
}]);

{{items[$index].name}
{{items[$index+1].name}
{{items[$index+2].name}

如果出现
情况,可以使用
ng。最后两个空白复选框是由于
项[$index+1]
项[$index+2]
引起的。由于您有
$index%3==0
并不意味着
$index+1
$index+2
存在,因此您可以使用
ng if
条件来检查
项数组中是否存在该值

var-app=angular.module('app',[]);
app.controller('TestController',['$scope',函数($scope){
$scope.items=[
{id:0,名称:“1/4英里”},
{id:1,名称:“1/2英里”},
{id:2,名字:“1英里”},
{id:3,名字:“2英里”},
{id:4,名字:“3英里”},
{id:5,名字:“4英里”},
{id:6,名字:“5英里”}
];
}]);

{{items[$index].name}
{{items[$index+1].name}
{{items[$index+2].name}

检查
$index+1
是否小于
项目。长度
(与
+2
复选框相同)谢谢,它工作了。检查
$index+1
是否小于
项目。长度
(与
+2
复选框相同)谢谢,它工作了。