I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.
My question was:
The problem I’m facing, as outlined in the code, is that I can’t seem to find out how to bind the second checkboxes (created with ng-repeat) to some model which I would then be able to use further down the code (showing/hiding yet another set of options). Also, I managed to show the additional number of inputs based on the count
parameter in the $scope.availableOptions
by using the $scope.getItterator
function, but now I don’t see how would I access the values of these checkboxes? I know I have to somehow use the option “ng-model” but am failing to do so, so any help appreciated.
My code is here, but am showing it here too:
HTML:
<div ng-controller='MyCtrl'> Show additional options <input type="checkbox" ng-model="additionalOptions"> <div ng-show="additionalOptions"> <ul> <li ng-repeat="option in availableOptions"> <label class="checkbox" for="opt_{{option.id}}"> <input type="checkbox" name="opt_{{option.id}}" id="opt_{{option.}}" /> {{option.name}} <ul ng-show="if the upper checkbox is clicked"> <li> <input type="text" ng-repeat="i in getItterator(option.count)" ng-model="SOME_VALUE_TO_BE_USED_BELOW"/> Output the value based on what's entered above in the textboxes (SOME_VALUE_TO_BE_USED_BELOW) </li> </ul> </label> </li> </ul> </div> </div>
and my js:
function MyCtrl($scope) { $scope.availableOptions = [{"id":"1","name":"easy","count":"2"},{"id":"2","name":"medium","count":"3"},{"id":"3","name":"hard","count":"2"}]; $scope.getItterator=function(n){ var a = new Array(); for(var i=1; i <= n; i++) a.push(i); return a; }; }
The answer, by user Khanh TO, was:
Try
ng-model="option.checked"
:<input type="checkbox" name="opt_{{option.id}}" id="opt_{{option.}}" ng-model="option.checked"/>And then you can access this property further below like this:
<ul ng-show="option.checked">If you need to also reference the text box inputs. You create a new property (
values
) on the option. Like this:{"id":"1","name":"easy","count":"2",values:[]}Html:
<div ng-repeat="i in getItterator(option.count)"> <input type="text" ng-model="option.values[$index]"/> {{option.values[$index]}} </div>