Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a tabel with rows with checboxes in it. When I click on a checkbox, I want the value of the ID-attribute to be pushed into the object.

I have the following HTML-code:

<tr ng-repeat="info in test">
    <td>{{info.stracka}}</td>
    <td>{{info.tid}}</td>
    <td><input type="checkbox" id="{{info.id}}" ng-model="compare.checkbox">
</tr>

As you can see, my ng-model connected to a compare object. How do I add the value of the ID-attribute to my compare object? And the ng-model seems to only be able to bind the value of one single checkbox to a variable in the controller. How can I solve this?

Here is my controller:

as.controller('Test', function($scope, $http, $rootScope, testFactory)
{   
    $http.get($rootScope.appUrl + '/nao/test/test')
        .success(function(data, status, headers, config) {
            $scope.test = data.data;
    });

    $scope.form = {};
    $scope.compare = {};
    $scope.submitForm = function(isValid) {
        if(isValid) 
        {   
            /*testFactory.testFactoryMethod(function($http) {
                $scope.test = data;
            });*/
            $http.post($rootScope.appUrl + '/nao/test', $scope.form)
            .success(function(data, status, headers, config) {
                console.log(data);
                $scope.test.push($scope.form);
                $scope.test.push(data);
            }).error(function(data, status) {

            });
        }
    };
});
share|improve this question
    
try ng-checked to manage the check – chf Jul 17 '14 at 14:09
    
@chf: How do I get the value of the ID-attribut into the object? – user500468 Jul 17 '14 at 14:10
    
Do you want to have ID-attribute value inside $scope.compare.checkbox variable? – Kasyx Jul 17 '14 at 14:10

Your problem is you're binding all the checkboxes to compare.checkbox

if you add $scope.compare.checkbox = []; to the controller and instead use

ng-model="compare.checkbox[info.tid]

assuming info.tid is a number and is unique you should be able to access the checkbox from the controller by using

$scope.compare.checkbox[thetidhere];

share|improve this answer
    
When I write $scope.compare.checkbox = []; I get this error messagE: Error: $scope.compare is undefined – user500468 Jul 17 '14 at 14:21
    
you first need to $scope.compare = {}; then $scope.compare.checkbox = []; – DelGiudice Jul 17 '14 at 14:54
    
How can I see ALL the ids in $scope.compare? – user500468 Jul 17 '14 at 14:59
    
Loop through the array using a for loop – DelGiudice Jul 17 '14 at 21:05

To push additional data you should use ng-click in following way:

<input 
    type="checkbox" id="{{info.id}}" 
    ng-model="compare.checkbox" 
    ng-click="compare.id_attribute = info.id" 
/>
share|improve this answer
    
Thank you. How should the controller look like? When I click on a checkbox, all the checboxes are selected instead, which is wrong. – user500468 Jul 17 '14 at 14:17
    
Thats because you are working with only one model for all of them. I dont know much about your application, but maybe it will be enought to delete model for this checkboxs. Remove ng-model="compare.checkbox" – Kasyx Jul 17 '14 at 16:27

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.