Search code examples
javascripthtmlangularjsangularjs-scopeangularjs-ng-repeat

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


Solution

  • 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