如何使用AngularJS绑定到复选框值列表?

nic*_*ine 662 javascript angularjs

我有几个复选框:

<input type='checkbox' value="apple" checked>
<input type='checkbox' value="orange">
<input type='checkbox' value="pear" checked>
<input type='checkbox' value="naartjie">
Run Code Online (Sandbox Code Playgroud)

我想绑定到我的控制器中的列表,以便每当更改复选框时,控制器都会维护所有已检查值的列表,例如,['apple', 'pear'].

ng-model似乎只能将一个复选框的值绑定到控制器中的变量.

还有另一种方法可以将四个复选框绑定到控制器中的列表吗?

Yos*_*shi 920

有两种方法可以解决这个问题.使用简单数组或对象数组.每种解决方案都有它的优点和缺点.下面你会发现每个案例一个.


用简单的数组作为输入数据

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>
Run Code Online (Sandbox Code Playgroud)

适当的控制器代码将是:

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);
    }
  };
}]);
Run Code Online (Sandbox Code Playgroud)

优点:简单的数据结构和按名称切换很容易处理

缺点:添加/删除很麻烦,因为必须管理两个列表(输入和选择)


使用对象数组作为输入数据

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>
Run Code Online (Sandbox Code Playgroud)

适当的控制器代码将是:

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);
}]);
Run Code Online (Sandbox Code Playgroud)

优点:添加/删除非常简单

缺点:更复杂的数据结构和按名称切换是麻烦的或需要帮助方法


演示:http://jsbin.com/ImAqUC/1/

  • `value ="{{fruit.name}}"`和`ng-checked ="fruit.checked"`是多余的,因为使用了ng-model. (24认同)
  • FYI,而不是注入$ filter,你可以注入filterFilter,然后使用如下:return filterFilter($ scope.fruits,{checked:true}); 内置和自定义过滤器在$ injector注册,名称为filterNameFilter("filterName"应为斜体) - [$ filterProvider docs](http://docs.angularjs.org/api/ng.$filterProvider) (10认同)
  • 我注意到没有必要在模型中指定"checked",Angular会自动设置属性:) (3认同)
  • 应该使用ng-change而不是ng-click,因为它更好地处理边缘情况. (3认同)
  • 我不知道 `name="selectedFruits[]"` 是做什么的?@Yoshi 你能指点一下吗? (2认同)
  • @ViktorMolokostov如果你提交*传统的*表格,那将是有用的.意思是将其发布到动作处理程序(某些服务器端脚本).使用php,具有类似名称的表单元素(使用方括号)在请求数据中创建一个数组.这样您就可以轻松处理所选的水果. (2认同)

kol*_*pto 405

简单的解决方案:

<div ng-controller="MainCtrl">
  <label ng-repeat="(color,enabled) in colors">
      <input type="checkbox" ng-model="colors[color]" /> {{color}} 
  </label>
  <p>colors: {{colors}}</p>
</div>

<script>
  var app = angular.module('plunker', []);

  app.controller('MainCtrl', function($scope){
      $scope.colors = {Blue: true, Orange: true};
  });
</script>
Run Code Online (Sandbox Code Playgroud)

http://plnkr.co/edit/U4VD61?p=preview

  • @kolypto - 这绝对是答案.我为那些正在使用对象的人(比如我)改写了它:http://plnkr.co/edit/cqsADe8lKegsBMgWMyB8?p = preview (57认同)
  • 虽然我非常喜欢这个答案!我认为,使用对象作为数据源存在一个主要问题.也就是说,因为根据定义,对象属性的顺序是未定义的,所以在显示复选框时无法提供明确的顺序.还是+1;) (10认同)
  • 我就像你一样做,但是`(颜色,启用)颜色中启用了什么? (5认同)
  • @Sebastian,因为`colors`是一个对象,当你迭代它时 - 你会得到一对`(key,value)`. (3认同)
  • http://plnkr.co/edit/u5PHlSqUfmqLp6k2meDg?p=preview用作列表过滤器 (3认同)
  • `colors`应命名为`isSelected`,它更容易阅读`isSelected [color]`而不是`colors [color]` (2认同)

Umu*_*acı 83

<input type='checkbox' ng-repeat="fruit in fruits"
  ng-checked="checkedFruits.indexOf(fruit) != -1" ng-click="toggleCheck(fruit)">
Run Code Online (Sandbox Code Playgroud)

.

function SomeCtrl ($scope) {
    $scope.fruits = ["apple, orange, pear, naartjie"];
    $scope.checkedFruits = [];
    $scope.toggleCheck = function (fruit) {
        if ($scope.checkedFruits.indexOf(fruit) === -1) {
            $scope.checkedFruits.push(fruit);
        } else {
            $scope.checkedFruits.splice($scope.checkedFruits.indexOf(fruit), 1);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

  • 喜欢这是多么简单,正是我正在寻找的东西(虽然我必须承认@vitalets指令是惊人的).我已经修改了一些Umur的代码来创建这个小提琴:https://jsfiddle.net/samurai_jane/9mwsbfuc/ (2认同)

Mic*_*ley 81

这是一个快速的小型可重用指令,似乎可以做你想做的事情.我简单地称之为checkList.它会在复选框更改时更新阵列,并在阵列更改时更新复选框.

app.directive('checkList', function() {
  return {
    scope: {
      list: '=checkList',
      value: '@'
    },
    link: function(scope, elem, attrs) {
      var handler = function(setup) {
        var checked = elem.prop('checked');
        var index = scope.list.indexOf(scope.value);

        if (checked && index == -1) {
          if (setup) elem.prop('checked', false);
          else scope.list.push(scope.value);
        } else if (!checked && index != -1) {
          if (setup) elem.prop('checked', true);
          else scope.list.splice(index, 1);
        }
      };

      var setupHandler = handler.bind(null, true);
      var changeHandler = handler.bind(null, false);

      elem.bind('change', function() {
        scope.$apply(changeHandler);
      });
      scope.$watch('list', setupHandler, true);
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

这是一个控制器和一个显示如何使用它的视图.

<div ng-app="myApp" ng-controller='MainController'>
  <span ng-repeat="fruit in fruits">
    <input type='checkbox' value="{{fruit}}" check-list='checked_fruits'> {{fruit}}<br />
  </span>

  <div>The following fruits are checked: {{checked_fruits | json}}</div>

  <div>Add fruit to the array manually:
    <button ng-repeat="fruit in fruits" ng-click='addFruit(fruit)'>{{fruit}}</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
app.controller('MainController', function($scope) {
  $scope.fruits = ['apple', 'orange', 'pear', 'naartjie'];
  $scope.checked_fruits = ['apple', 'pear'];
  $scope.addFruit = function(fruit) {
    if ($scope.checked_fruits.indexOf(fruit) != -1) return;
    $scope.checked_fruits.push(fruit);
  };
});
Run Code Online (Sandbox Code Playgroud)

(按钮表明更改阵列也会更新复选框.)

最后,这是一个关于Plunker的指令实例:http://plnkr.co/edit/3YNLsyoG4PIBW6Kj7dRK?p = preview

  • 感谢布兰登,这完全符合我的要求(而且正是问题所要求的,与其他答案不同).我做的唯一调整是将你的"elem.on('change',function()..."更改为"elem.bind('change',function()..."以便删除对jQuery的依赖. (2认同)
  • 如果您遇到AngularJS> = 1.4.4的问题,请检查https://github.com/angular/angular.js/issues/13037:将'value:'@'替换为'value:'= ngValue'` (2认同)

vit*_*ets 66

基于此线程中的答案,我创建了涵盖所有情况的清单模型指令:

  • 简单的基元数组
  • 对象数组(选择id或整个对象)
  • 对象属性迭代

对于主题启动器案例,它将是:

<label ng-repeat="fruit in ['apple', 'orange', 'pear', 'naartjie']">
    <input type="checkbox" checklist-model="selectedFruits" checklist-value="fruit"> {{fruit}}
</label>
Run Code Online (Sandbox Code Playgroud)


小智 11

使用字符串$index可以帮助使用所选值的散列映射:

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

这样,ng-model对象将使用表示索引的键进行更新.

$scope.someObject = {};
Run Code Online (Sandbox Code Playgroud)

过了一会儿$scope.someObject应该看起来像:

$scope.someObject = {
     0: true,
     4: false,
     1: true
};
Run Code Online (Sandbox Code Playgroud)

此方法不适用于所有情况,但易于实现.


Tos*_*osh 8

我认为最简单的解决方法是使用'select'并指定'multiple':

<select ng-model="selectedfruit" multiple ng-options="v for v in fruit"></select>
Run Code Online (Sandbox Code Playgroud)

否则,我认为你必须处理列表来构造列表(通过$watch()模型数组绑定复选框).

  • 他要求一个复选框列表,但你告诉他有关选项的选项.这是完全不同的. (3认同)

Mar*_*cok 8

由于您接受了未使用列表的答案,我将假设我的评论问题的答案是"不,它不必是列表".我也有这样的印象,也许你是在渲染HTML服务器端,因为样本HTML中存在"已检查"(如果使用ng-model对复选框进行建模,则不需要这样做).

无论如何,这是我在问这个问题时的想法,也假设您正在生成HTML服务器端:

<div ng-controller="MyCtrl" 
 ng-init="checkboxes = {apple: true, orange: false, pear: true, naartjie: false}">
    <input type="checkbox" ng-model="checkboxes.apple">apple
    <input type="checkbox" ng-model="checkboxes.orange">orange
    <input type="checkbox" ng-model="checkboxes.pear">pear
    <input type="checkbox" ng-model="checkboxes.naartjie">naartjie
    <br>{{checkboxes}}
</div>
Run Code Online (Sandbox Code Playgroud)

ng-init允许服务器端生成的HTML初始设置某些复选框.

小提琴.

  • 天啊!这不是一个坏习惯,它是最糟糕的一个! (2认同)

Ada*_*dam 5

我已经改编了Yoshi接受的处理复杂对象(而不是字符串)的答案.

HTML

<div ng-controller="TestController">
    <p ng-repeat="permission in allPermissions">
        <input type="checkbox" ng-checked="selectedPermissions.containsObjectWithProperty('id', permission.id)" ng-click="toggleSelection(permission)" />
        {{permission.name}}
    </p>

    <hr />

    <p>allPermissions: | <span ng-repeat="permission in allPermissions">{{permission.name}} | </span></p>
    <p>selectedPermissions: | <span ng-repeat="permission in selectedPermissions">{{permission.name}} | </span></p>
</div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

Array.prototype.indexOfObjectWithProperty = function(propertyName, propertyValue)
{
    for (var i = 0, len = this.length; i < len; i++) {
        if (this[i][propertyName] === propertyValue) return i;
    }

    return -1;
};


Array.prototype.containsObjectWithProperty = function(propertyName, propertyValue)
{
    return this.indexOfObjectWithProperty(propertyName, propertyValue) != -1;
};


function TestController($scope)
{
    $scope.allPermissions = [
    { "id" : 1, "name" : "ROLE_USER" },
    { "id" : 2, "name" : "ROLE_ADMIN" },
    { "id" : 3, "name" : "ROLE_READ" },
    { "id" : 4, "name" : "ROLE_WRITE" } ];

    $scope.selectedPermissions = [
    { "id" : 1, "name" : "ROLE_USER" },
    { "id" : 3, "name" : "ROLE_READ" } ];

    $scope.toggleSelection = function toggleSelection(permission) {
        var index = $scope.selectedPermissions.indexOfObjectWithProperty('id', permission.id);

        if (index > -1) {
            $scope.selectedPermissions.splice(index, 1);
        } else {
            $scope.selectedPermissions.push(permission);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

工作示例:http://jsfiddle.net/tCU8v/


Adr*_*scu 5

另一个简单的指令可能是:

var appModule = angular.module("appModule", []);

appModule.directive("checkList", [function () {
return {
    restrict: "A",
    scope: {
        selectedItemsArray: "=",
        value: "@"
    },
    link: function (scope, elem) {
        scope.$watchCollection("selectedItemsArray", function (newValue) {
            if (_.contains(newValue, scope.value)) {
                elem.prop("checked", true);
            } else {
                elem.prop("checked", false);
            }
        });
        if (_.contains(scope.selectedItemsArray, scope.value)) {
            elem.prop("checked", true);
        }
        elem.on("change", function () {
            if (elem.prop("checked")) {
                if (!_.contains(scope.selectedItemsArray, scope.value)) {
                    scope.$apply(
                        function () {
                            scope.selectedItemsArray.push(scope.value);
                        }
                    );
                }
            } else {
                if (_.contains(scope.selectedItemsArray, scope.value)) {
                    var index = scope.selectedItemsArray.indexOf(scope.value);
                    scope.$apply(
                        function () {
                            scope.selectedItemsArray.splice(index, 1);
                        });
                }
            }
            console.log(scope.selectedItemsArray);
        });
    }
};
}]);
Run Code Online (Sandbox Code Playgroud)

控制器:

appModule.controller("sampleController", ["$scope",
  function ($scope) {
    //#region "Scope Members"
    $scope.sourceArray = [{ id: 1, text: "val1" }, { id: 2, text: "val2" }];
    $scope.selectedItems = ["1"];
    //#endregion
    $scope.selectAll = function () {
      $scope.selectedItems = ["1", "2"];
  };
    $scope.unCheckAll = function () {
      $scope.selectedItems = [];
    };
}]);
Run Code Online (Sandbox Code Playgroud)

和HTML:

<ul class="list-unstyled filter-list">
<li data-ng-repeat="item in sourceArray">
    <div class="checkbox">
        <label>
            <input type="checkbox" check-list selected-items-array="selectedItems" value="{{item.id}}">
            {{item.text}}
        </label>
    </div>
</li>
Run Code Online (Sandbox Code Playgroud)

我还包括一个Plunker:http://plnkr.co/edit/XnFtyij4ed6RyFwnFN6V?p = preview


Moh*_*eer 5

以下解决方案似乎是一个不错的选择,

<label ng-repeat="fruit in fruits">
  <input
    type="checkbox"
    ng-model="fruit.checked"
    ng-value="true"
  > {{fruit.fruitName}}
</label>
Run Code Online (Sandbox Code Playgroud)

而在控制器模型中,值fruits将是这样的

$scope.fruits = [
  {
    "name": "apple",
    "checked": true
  },
  {
    "name": "orange"
  },
  {
    "name": "grapes",
    "checked": true
  }
];
Run Code Online (Sandbox Code Playgroud)