Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 AngularJS:计算更改时选定的项目_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS:计算更改时选定的项目

Javascript AngularJS:计算更改时选定的项目,javascript,angularjs,Javascript,Angularjs,我有一个带有一些自定义控件的窗体。在这个表单的底部,我需要显示“numSelected of numRequired”。我知道“numRequired”,但我怎么能算出“numSelected” 该表单如下所示: <div ng-controller="MyCtrl"> <fieldset ng-repeat="item in items"> <div class="buttons" ng-init="selectedVal=1">

我有一个带有一些自定义控件的窗体。在这个表单的底部,我需要显示“numSelected of numRequired”。我知道“numRequired”,但我怎么能算出“numSelected”

该表单如下所示:

<div ng-controller="MyCtrl">
    <fieldset ng-repeat="item in items">
        <div class="buttons" ng-init="selectedVal=1">
            <label>
                <input type="radio" name="item{{$index}}" ng-model="selectedVal" value="1" ng-selected="selectedVal==1">
            </label>
            <label>
                <input type="radio" name="item{{$index}}" ng-model="selectedVal" value="0" ng-selected="selectedVal==0">
            </label>
        </div>
    </fieldset>
    <div>{{numSelected}} of {{numRequired}}</div>
</div>

{{numRequired}中的{numSelected}}
我曾尝试将
ng click
ng change
绑定到每个输入,并在
MyCtrl
中进行计数,但我想我不知道如何计算满足条件的项目。

在范围内进行计数

 $scope.count = 0;
    $scope.selectedIndex = {};
    $scope.count = function (index){
        $scope.selectedIndex[index] = 1;
        updateCount();
    }
    $scope.uncount = function (index){
        delete  $scope.selectedIndex[index];
        updateCount();
    }
    function updateCount(){
            var element_count =0;
             for (e in $scope.selectedIndex)
              { 
                element_count++; 
              }
         $scope.count = element_count;
    }
然后单击下面的按钮

  <input type="radio" ng-click="count({{$index}})" name="item{{$index}}" ng-model="selectedVal" value="1" ng-selected="selectedVal==1">

 <input type="radio" ng-click="uncount({{$index}})" name="item{{$index}}" ng-model="selectedVal" value="1" ng-selected="selectedVal==1">

在这里显示计数

<div>{{count}} of {{numRequired}}</div>
{{numRequired}的{code>{count} 或者您可以使用ng change直接更改计数

<input type="radio" ng-change={{count + 1}} name="item{{$index}}" ng-model="selectedVal" value="1" ng-selected="selectedVal==1">

<input type="radio" ng-change={{count -1}} name="item{{$index}}" ng-model="selectedVal" value="1" ng-selected="selectedVal==1">


如果用户不停地反复单击同一个项目会怎么样?他们仍然会选择一个项目,但数量将继续攀升。我已经编辑了“我的答案”以处理多次单击,或者你可以尝试更改,而不是在回答之前单击。我使用ng change和一个计算+/-的方法。