Home > Web Front-end > JS Tutorial > body text

How to Bind AngularJS Checkboxes to a List of Values?

Linda Hamilton
Release: 2024-11-24 17:33:14
Original
482 people have browsed it

How to Bind AngularJS Checkboxes to a List of Values?

Binding AngularJS Checkboxes to List of Values

Problem:

In AngularJS, a list of checkboxes is presented, and the objective is to establish a binding between them and a list in the controller. Each checked checkbox indicates the inclusion of its associated value in the list.

Solution:

AngularJS provides two viable solutions to this problem:

1. Simple Array as Input Data

In this approach, the HTML structure mimics the checkbox list:

<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>
Copy after login

The accompanying controller code handles the interaction:

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);

    if (idx > -1) {
      $scope.selection.splice(idx, 1);
    } else {
      $scope.selection.push(fruitName);
    }
  };
}]);
Copy after login

2. Object Array as Input Data

Using an object array as the input data adds additional complexity but simplifies insertion and deletion operations:

<label ng-repeat="fruit in fruits">
  <input
    type="checkbox"
    name="selectedFruits[]"
    value="{{fruit.name}}"
    ng-model="fruit.selected"
  > {{fruit.name}}
</label>
Copy after login

The controller code reflects the change:

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 });
  };

  $scope.$watch('fruits|filter:{selected:true}', function (nv) {
    $scope.selection = nv.map(function (fruit) {
      return fruit.name;
    });
  }, true);
}]);
Copy after login

Advantages and Disadvantages:

  • Simple Array:

    • Easy data structure
    • Easy toggling by name
    • However, cumbersome insertion and deletion
  • Object Array:

    • More complex data structure
    • Toggles by name require helper methods
    • However, insertion and deletion are very simple

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

The above is the detailed content of How to Bind AngularJS Checkboxes to a List of Values?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template