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

前端 未结 29 2191
天涯浪人
天涯浪人 2020-11-22 05:20

I have a few checkboxes:





        
相关标签:
29条回答
  • 2020-11-22 06:06

    Inspired from Yoshi's post above. Here is the plnkr.

    (function () {
       
       angular
          .module("APP", [])
          .controller("demoCtrl", ["$scope", function ($scope) {
             var dc = this
             
             dc.list = [
                "Selection1",
                "Selection2",
                "Selection3"
             ]
    
             dc.multipleSelections = []
             dc.individualSelections = []
             
             // Using splice and push methods to make use of 
             // the same "selections" object passed by reference to the 
             // addOrRemove function as using "selections = []" 
             // creates a new object within the scope of the 
             // function which doesn't help in two way binding.
             dc.addOrRemove = function (selectedItems, item, isMultiple) {
                var itemIndex = selectedItems.indexOf(item)
                var isPresent = (itemIndex > -1)
                if (isMultiple) {
                   if (isPresent) {
                      selectedItems.splice(itemIndex, 1)
                   } else {
                      selectedItems.push(item)
                   }
                } else {
                   if (isPresent) {
                      selectedItems.splice(0, 1)
                   } else {
                      selectedItems.splice(0, 1, item)
                   }
                }
             }
             
          }])
       
    })()
    label {
      display: block;  
    }
    <!DOCTYPE html>
    <html>
    
       <head>
          <link rel="stylesheet" href="style.css" />
       </head>
    
       <body ng-app="APP" ng-controller="demoCtrl as dc">
          <h1>checkbox-select demo</h1>
          
          <h4>Multiple Selections</h4>
          <label ng-repeat="thing in dc.list">
             <input 
                type="checkbox" 
                ng-checked="dc.multipleSelections.indexOf(thing) > -1"
                ng-click="dc.addOrRemove(dc.multipleSelections, thing, true)"
             > {{thing}}
          </label>
          
          <p>
             dc.multipleSelections :- {{dc.multipleSelections}}
          </p>
          
          <hr>
          
          <h4>Individual Selections</h4>
          <label ng-repeat="thing in dc.list">
             <input 
                type="checkbox" 
                ng-checked="dc.individualSelections.indexOf(thing) > -1"
                ng-click="dc.addOrRemove(dc.individualSelections, thing, false)"
             > {{thing}}
          </label>
          
          <p>
             dc.invidualSelections :- {{dc.individualSelections}}
          </p>
          
          <script data-require="jquery@3.0.0" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
          <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
          <script src="script.js"></script>
       </body>
    
    </html>

    0 讨论(0)
  • 2020-11-22 06:06

    A simple HTML only way of doing it:

    <input type="checkbox"
           ng-checked="fruits.indexOf('apple') > -1"
           ng-click="fruits.indexOf('apple') > -1 ? fruits.splice(fruits.indexOf('apple'), 1) : fruits.push('apple')">
    <input type="checkbox"
           ng-checked="fruits.indexOf('orange') > -1"
           ng-click="fruits.indexOf('orange') > -1 ? fruits.splice(fruits.indexOf('orange'), 1) : fruits.push('orange')">
    <input type="checkbox"
           ng-checked="fruits.indexOf('pear') > -1"
           ng-click="fruits.indexOf('pear') > -1 ? fruits.splice(fruits.indexOf('pear'), 1) : fruits.push('pear')">
    <input type="checkbox"
           ng-checked="fruits.indexOf('naartjie') > -1"
           ng-click="fruits.indexOf('apple') > -1 ? fruits.splice(fruits.indexOf('apple'), 1) : fruits.push('naartjie')">

    0 讨论(0)
  • 2020-11-22 06:07

    You don't have to write all that code. AngularJS will keep the model and the checkboxes in sync simply by using ngTrueValue and ngFalseValue

    Codepen here: http://codepen.io/paulbhartzog/pen/kBhzn

    Code snippet:

    <p ng-repeat="item in list1" class="item" id="{{item.id}}">
      <strong>{{item.id}}</strong> <input name='obj1_data' type="checkbox" ng-model="list1[$index].data" ng-true-value="1" ng-false-value="0"> Click this to change data value below
    </p>
    <pre>{{list1 | json}}</pre>
    
    0 讨论(0)
  • 2020-11-22 06:07

    Take a look this: checklist-model.

    It works with JavaScript arrays, and objects and it can use static HTML checkboxes, without ng-repeat

    <label><input type="checkbox" checklist-model="roles" value="admin"> Administrator</label>
    <label><input type="checkbox" checklist-model="roles" value="customer"> Customer</label>
    <label><input type="checkbox" checklist-model="roles" value="guest"> Guest</label>
    <label><input type="checkbox" checklist-model="roles" value="user"> User</label>
    

    And the JavaScript side:

    var app = angular.module("app", ["checklist-model"]);
    app.controller('Ctrl4a', function($scope) {
        $scope.roles = [];
    });
    
    0 讨论(0)
  • 2020-11-22 06:08

    Using a string of $index can help to use a hashmap of selected values:

    <ul>
        <li ng-repeat="someItem in someArray">
            <input type="checkbox" ng-model="someObject[$index.toString()]" />
        </li>
    </ul>
    

    This way the ng-model object gets updated with the key representing the index.

    $scope.someObject = {};
    

    After a while $scope.someObject should look something like:

    $scope.someObject = {
         0: true,
         4: false,
         1: true
    };
    

    This method won't work for all situations, but it is easy to implement.

    0 讨论(0)
  • 2020-11-22 06:09

    Check out this directive that manages effectively lists of checkboxes. I hope it works for you. CheckList Model

    0 讨论(0)
提交回复
热议问题