Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS add value of checkboxes to an array

I have this code:

<tr ng-repeat="doc in providers">      
  <td><input type="checkbox" ng-true-value="{{doc.provider.Id}}" ng-false-value="" ng-model="ids"></td> 
</tr>

{{ids}}

i want to get the values of the checkboxes on an array

like image 923
Rodrigo Zurek Avatar asked Jun 10 '13 18:06

Rodrigo Zurek


People also ask

How to store checkbox value in array in Angular?

You should save your checkbox by the position of the current title, right now your code is to push the boolean into the array, which couldn't tell you what value it stand for actually. Whenever you select the checkbox, we just simply reverse its value in the array.

How do I bind to list of checkbox values with Angularjs?

directive('checkList', function() { return { scope: { list: '=checkList', value: '@' }, link: function(scope, elem, attrs) { var handler = function(setup) { var checked = elem. prop('checked'); var index = scope. list. indexOf(scope.

How can use implode function in checkbox in PHP?

You need to do the following: $check = isset($_POST['check']) ? $_POST['check'] : ''; $check_msg = is_array($check) ? implode(", ", $check) : ''; $body = "F-Name: ".


1 Answers

ng-true-value only accepts strings so you'll need to use a workaround. This has been a feature request for some time. In the meantime, you can do this:

Create an ids object in the controller like:

$scope.ids = {};

and change ng-model to reference a key in that object. You can use the default true/false checkbox values:

<td><input type="checkbox" ng-model="ids[doc.provider.Id]"></td>

Then you can loop over the keys in ids checking for true.

Here is a fiddle

like image 51
Dan Avatar answered Oct 21 '22 23:10

Dan