3

I have a form which has 10 checkboxes. By default angular js triggers on individual checkbox. I want to grab all selected check box values on submit action only. Here is my code...

<form name="thisform" novalidate data-ng-submit="booking()">
    <div ng-repeat="item in items" class="standard" flex="50">
        <label>
            <input type="checkbox"  ng-model="typeValues[item._id]" value="{{item._id}}"/>
            {{ item.Service_Categories}}
        </label>
    </div>
    <input type="submit" name="submit" value="submit"/>
</form>

$scope.check= function() { 
    //console.log("a");
    $http.get('XYZ.com').success(function(data, status,response) { 
    $scope.items=data;
});

$scope.booking=function(){
    $scope.typeValues = [];
    console.log($scope.typeValues);
}

I am getting empty array.

Can somebody tell how to grab all selected checkbox values only on submit event.

4 Answers 4

4
<div ng-repeat="item in items">
    <input type="checkbox" ng-model="item.SELECTED"  ng-true-value="Y" ng-false-value="N"/>
</div>
<input type="submit" name="submit" value="submit" ng-click="check(items)"/>

$scope.check= function(data) { 
    var arr = [];
    for(var i in data){
       if(data[i].SELECTED=='Y'){
           arr.push(data[i].id);
       }
    }
    console.log(arr);
    // Do more stuffs here
}
Sign up to request clarification or add additional context in comments.

5 Comments

actually im new to angularjs .i am getting error like this Error: [ngModel:constexpr] errors.angularjs.org/1.3.13/ngModel/… at Error (native) at ajax.googleapis.com/ajax/libs/angularjs/1.3.13/…
script> angular.module('info', []) .controller('FormController', ['$scope','$http','$window','$location', function($scope,$http,$window,$location,$stateProvider) { $scope.selected = []; $scope.items = [{id:1},{id:2}]; $scope.check= function(data) { var arr = []; for(var i in data){ if(data[i].SELECTED=='Y'){ arr.push(data[i].id); } } console.log(arr); $scope.selected = arr; // Do more stuffs here } }]);my controller i am getting same error ngModel:constexpr
Try Using ng-true-value="'Y'" ng-false-value="'N'" in html
<input type="checkbox" ng-model="item.SELECTED" ng-true-value="'Y'" ng-false-value="'N'"/>{{item}} it's working for me thanks
how can i pass data to mongodb like this ['Wash', 'Tyres', 'Spares', 'Accessories'];
1

Can I suggest reading the answer I posted yesterday to a similar StackOverflow question..

AngularJS with checkboxes

This displayed a few checkboxes, then bound them to an array, so we would always know which of the boxes were currently checked.

enter image description here

And yes, you could ignore the contents of this bound variable until the submit button was pressed, if you wanted to.

Comments

0

As per your code all the checkboxes values will be available in the typeValues array. You can use something like this in your submit function:

$scope.typeValues

If you want to access the value of 3rd checkbox then you need to do this:

var third = $scope.typeValues[2];

Comments

0

Declare your ng-model as array in controller, like

$scope.typeValues = [];

And in your template, please use

ng-model="typeValues[item._id]"

And in your controller, you will get this model array values in terms of 0 and 1. You can iterate over there.

3 Comments

i am getting empty array
Are you getting an array with total items, which you had in items object ?
i am getting total length

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.