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

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

I have a few checkboxes:





        
29条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 06:21

    Based on my other post here, I have made a reusable directive.

    Check out the GitHub repository

    (function () {
       
       angular
          .module("checkbox-select", [])
          .directive("checkboxModel", ["$compile", function ($compile) {
             return {
                restrict: "A",
                link: function (scope, ele, attrs) {
                   // Defining updateSelection function on the parent scope
                   if (!scope.$parent.updateSelections) {
                      // 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.
                      scope.$parent.updateSelections = 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)
                            }
                         }
                      }   
                   }
                   
                   // Adding or removing attributes
                   ele.attr("ng-checked", attrs.checkboxModel + ".indexOf(" + attrs.checkboxValue + ") > -1")
                   var multiple = attrs.multiple ? "true" : "false"
                   ele.attr("ng-click", "updateSelections(" + [attrs.checkboxModel, attrs.checkboxValue, multiple].join(",") + ")")
                   
                   // Removing the checkbox-model attribute, 
                   // it will avoid recompiling the element infinitly
                   ele.removeAttr("checkbox-model")
                   ele.removeAttr("checkbox-value")
                   ele.removeAttr("multiple")
                   
                   $compile(ele)(scope)
                }
             }
          }])
       
          // Defining app and controller
          angular
          .module("APP", ["checkbox-select"])
          .controller("demoCtrl", ["$scope", function ($scope) {
             var dc = this
             dc.list = [
                "selection1",
                "selection2",
                "selection3"
             ]
             
             // Define the selections containers here
             dc.multipleSelections = []
             dc.individualSelections = []
          }])
       
    })()
    label {
      display: block;  
    }
    
    
    
       
          
          
       
       
       
          

    checkbox-select demo

    Multiple Selections

    dc.multipleSelecitons:- {{dc.multipleSelections}}

    Individual Selections

    dc.individualSelecitons:- {{dc.individualSelections}}

提交回复
热议问题