imma imma - 2 months ago 5
AngularJS Question

Get Position of selected checkbox AngularJS

I want to retrieve the position of a checked value :

HTML:

<div ng-repeat="i in range(Data.nop)">
<div class="form-group">

<div class="col-md-6 col-xs-12">

Option {{i}})

<br>
<input type="checkbox" ng-model="score1[i-1]" ng-value="{{i-1}}" name="score1"/>
<br>
{{score1}}
<textarea ng-model="options[i-1]" class="form-control"></textarea>


</div>
</div>
</div>


Controller:

$scope.score1=[];

Answer

The following solves my initial problem:

HTML

<div ng-repeat="i in range(Data.nop)">
    <div class="form-group">

        <div class="col-md-6 col-xs-12">
            Option {{i}})
            <br>
            <input type="checkbox" ng-model="score1[i-1]"  name="score1" ng-value="{{i-1}}" ng-true-value="'checked'" ng-false-value="'unchecked'"/>
            <br>
            <textarea  ng-model="options[i-1]" class="form-control" ></textarea>
        </div>
    </div>
</div>

Controller

    $scope.options=[];
    $scope.score1=[];
    $scope.scores=[];

    angular.forEach($scope.score1,function(value,key){
        if (value ==='checked'){
            $scope.scores.push(key);
        }
    });
Comments