Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

While trying to implement Checkbox functionality,I am not able to obtain which checkboxes the user has selected. How is this done? Below is my HTML snippet:

<td>
                <table border="0"><th>Tier</th>
                <tr ng-repeat="item in tiertype">             
                    <td>
                    <label class="checkbox" for="{{item.id}}">
                    <input type="checkbox" ng-model="selection.ids[item.id]" name="tier_class" ng-checked="item.checked" id="{{item.id}}"/>{{item.name}}
                    </label>
                    </td></tr></table>
            </td>

And below is my javascript snippet:

$scope.tiertype = [ 
                       { id: 1, name: "All", checked: true}, 
                       { id: 2, name: "Standard", checked: false },
                       { id: 3, name: "Unreserved", checked: false },
                       { id: 3, name: "Reserved", checked: false },
                       { id: 4, name: "General", checked: false }
                       ];

Iam not getting proper values on using "check box value= {{selection.ids}}". Moreover, on checking any checbox, all are getting checked or the checbox with id:1 (default). Seems strange to me, not able to figure out. Where am I going wrong as I dont have much experience in AngularJS.

share|improve this question
 
Change your ngModel to ng-model="item.checked" –  CodeHater Sep 3 at 14:58
 
I've put together a jsFiddle that should help . Checkbox Fiddle The key is using a directive. –  Zack Argyle Sep 3 at 16:26
 
one problem of all checkboxes getting checked etc got solved by this but How can i know on the js side using scope etc to know which checkboxes were checked by user? –  Shrey Sep 3 at 16:28
add comment

1 Answer

Just set the checkbox ng-model to the boolean value for the check. IE: ng-model="item.checked"

share|improve this answer
 
ng-model ="item.checked" enables default check on 1st option but on doing "values checked by user: {{checked}}" or {{item.checked}}, doesnot return anything. How would i come to know which checkboxes were checked by user? –  Shrey Sep 3 at 16:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.