Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算角度中选定的复选框?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何计算角度中选定的复选框?

Javascript 如何计算角度中选定的复选框?,javascript,html,angularjs,Javascript,Html,Angularjs,我需要计算我在列表中选择的项目数 我有以下清单: <ul> <li ng-repeat="items in item"> <input type="checkbox" name="item_id[]" /> </li> </ul> 如果您还有复选框,请选中所有复选框 <input type="checkbox" ng-model="selected" class="checkAll" ng-change="se

我需要计算我在列表中选择的项目数

我有以下清单:

<ul>
  <li ng-repeat="items in item">
    <input type="checkbox" name="item_id[]" />
  </li>
</ul>
如果您还有
复选框,请选中所有
复选框

<input type="checkbox"  ng-model="selected" class="checkAll" ng-change="selectAll(selected)" />

你使用ngRepeat看起来是错误的。它应该是“项目中的项目”,而不是相反。 此外,您的输入没有使用ng模型,这使得获得计数变得更加困难

因此,如果您添加ng模型,您可以通过多种不同的方式获得计数,其中之一是:

app.controller('AppController'),
[
“$scope”,
职能($范围){
$scope.items=[
{id:1,标题:“抓不住我们”},
{id:2,标题:“给我一个理由”},
{id:3,标题:“镜像”},
{id:4,标题:“走运”},
];
$scope.selectedItems=0;
$scope.$watch('items',函数(items){
var-selectedItems=0;
角度。forEach(项目、功能(项目){
selectedItems+=项目。所选?1:0;
})
$scope.selectedItems=selectedItems;
},对);
}
]
);

  • {{item.title}
所选项目长度:{selectedItems}
不要投反对票,除非你能评论为什么是+1 nevermind buddy,你的问题似乎没那么糟糕@Stevensee::啊,那只是我这边的一个输入错误。添加
ng型号时,它工作正常,谢谢。必须使用
ng模型
并不能真正从角度得到解释
<input type="checkbox"  ng-model="selected" class="checkAll" ng-change="selectAll(selected)" />
    $scope.selectAll = function (selected) {
        var items = $scope.items;
        angular.forEach(items, function (item) {
            item.selected = selected;
        });
        // Update the counter
        if(selected){
            $scope.selectedCounter = items.length;
        } else {
            $scope.selectedCounter = 0;
        }
    };