在AngularJS中绑定复选框或多选下拉数组?

时间:2013-05-20 08:08:07

标签: javascript data-binding angularjs html-select html.checkbox

我是AngularJS的新手,因此对于如何将复选框或多选下拉列表绑定到单独的列表感到困惑。

<body ng-controller="FooListCtrl">
  <span ng-repeat="foolist in foos">
    <select ng-model="selected" ng-options="foo for foo in foolist" multiple="">
    </select>
  </span>
</body>
'use strict';

function FooListCtrl($scope) {
    $scope.foos = {"Bar": [ "foo", "bar"]};
    $scope.selected = [];
}

FooListCtrl.$inject = ['$scope'];

运行代码:http://jsfiddle.net/gBcN2/

1 个答案:

答案 0 :(得分:2)

如果我做对了你想要的东西:

  1. 您没有ng-app定义。
  2. 如果您使用AngularJS作为外部资源,请在jsFiddle上获取AngularJS片段的No wrap - in <head>加载模式。
  3. 模型selected拥有自己的“范围”,因为您使用ng-repeat。要了解我的意思,这里是您的代码的固定版本: http://jsfiddle.net/gBcN2/2/
  4. 首先{{selected}}工作正常,但第二个是ng-repeat范围的“外部”。

    PS:
    如果您想像在示例中所写的那样使用ng-repeat,则不必使用{{1}}:quick fiddle of how I'd do it.

    修改
    对于复选框,它就是这样的 - http://jsfiddle.net/qQg8u/2/