Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

javascript - 如何使用AngularJS绑定到复选框值列表?(How do I bind to list of checkbox values with AngularJS?)

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'] .(我想绑定到我的控制器中的列表,这样每当复选框被更改时,控制器都会保留所有已检查值的列表,例如['apple', 'pear'] 。) ng-model seems to only be able to bind the value of one single checkbox to a variable in the controller.(ng-model似乎只能将一个复选框的值绑定到控制器中的变量。) Is there another way to do it so that I can bind the four checkboxes to a list in the controller?(还有另一种方法可以将四个复选框绑定到控制器中的列表吗?)   ask by nickponline translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:(HTML可能如下所示:) <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:(HTML可能如下所示:) <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/(演示http//jsbin.com/ImAqUC/1/)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...