Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:34:08+00:00 2026-06-17T21:34:08+00:00

I have a few checkboxes: <input type=’checkbox’ value=apple checked> <input type=’checkbox’ value=orange> <input type=’checkbox’

  • 0

I have a few checkboxes:

<input type='checkbox' value="apple" checked>
<input type='checkbox' value="orange">
<input type='checkbox' value="pear" checked>
<input type='checkbox' value="naartjie">

That I would like to bind to a list in my controller such that whenever a checkbox is changed the controller maintains a list of all the checked values, for example, ['apple', 'pear'].

ng-model seems to only be able to bind the value of one single checkbox to a variable in the controller.

Is there another way to do it so that I can bind the four checkboxes to a list in the controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:34:09+00:00Added an answer on June 17, 2026 at 9:34 pm

    There are two ways to approach this problem. Either use a simple array or an array of objects. Each solution has it pros and cons. Below you’ll find one for each case.


    With a simple array as input data

    The HTML could look like:

    <label ng-repeat="fruitName in fruits">
      <input
        type="checkbox"
        name="selectedFruits[]"
        value="{{fruitName}}"
        ng-checked="selection.indexOf(fruitName) > -1"
        ng-click="toggleSelection(fruitName)"
      > {{fruitName}}
    </label>
    

    And the appropriate controller code would be:

    app.controller('SimpleArrayCtrl', ['$scope', function SimpleArrayCtrl($scope) {
    
      // Fruits
      $scope.fruits = ['apple', 'orange', 'pear', 'naartjie'];
    
      // Selected fruits
      $scope.selection = ['apple', 'pear'];
    
      // Toggle selection for a given fruit by name
      $scope.toggleSelection = function toggleSelection(fruitName) {
        var idx = $scope.selection.indexOf(fruitName);
    
        // Is currently selected
        if (idx > -1) {
          $scope.selection.splice(idx, 1);
        }
    
        // Is newly selected
        else {
          $scope.selection.push(fruitName);
        }
      };
    }]);
    

    Pros: Simple data structure and toggling by name is easy to handle

    Cons: Add/remove is cumbersome as two lists (the input and selection) have to be managed


    With an object array as input data

    The HTML could look like:

    <label ng-repeat="fruit in fruits">
      <!--
        - Use `value="{{fruit.name}}"` to give the input a real value, in case the form gets submitted
          traditionally
    
        - Use `ng-checked="fruit.selected"` to have the checkbox checked based on some angular expression
          (no two-way-data-binding)
    
        - Use `ng-model="fruit.selected"` to utilize two-way-data-binding. Note that `.selected`
          is arbitrary. The property name could be anything and will be created on the object if not present.
      -->
      <input
        type="checkbox"
        name="selectedFruits[]"
        value="{{fruit.name}}"
        ng-model="fruit.selected"
      > {{fruit.name}}
    </label>
    

    And the appropriate controller code would be:

    app.controller('ObjectArrayCtrl', ['$scope', 'filterFilter', function ObjectArrayCtrl($scope, filterFilter) {
    
      // Fruits
      $scope.fruits = [
        { name: 'apple',    selected: true },
        { name: 'orange',   selected: false },
        { name: 'pear',     selected: true },
        { name: 'naartjie', selected: false }
      ];
    
      // Selected fruits
      $scope.selection = [];
    
      // Helper method to get selected fruits
      $scope.selectedFruits = function selectedFruits() {
        return filterFilter($scope.fruits, { selected: true });
      };
    
      // Watch fruits for changes
      $scope.$watch('fruits|filter:{selected:true}', function (nv) {
        $scope.selection = nv.map(function (fruit) {
          return fruit.name;
        });
      }, true);
    }]);
    

    Pros: Add/remove is very easy

    Cons: Somewhat more complex data structure and toggling by name is cumbersome or requires a helper method


    Demo: http://jsbin.com/ImAqUC/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a few checkboxes like the following: <input type=checkbox onchange=foo(bar); /> Bar
Here's the scenario: I have a contact form with few checkboxes: <input type=text name=cola[]
I have some code <input type=checkbox id=chk value=value /> <label for=chk>Value </label> <br/> <input
i have an app that has a few checkboxes in the settings and then
I have a view with a few checkboxes that can be selected or unselected.
I have a JTable with few columns that are painted as checkboxes. What I
I dynamically load in a few li's that have a label and a checkbox
I have a list a simple dialog box which contains a few checkboxes, I
i have form in drupal which uploads images and has got few checkboxes in
I have a set of checkboxes and have disabled few which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.