12

尝试在angularjs中选择关于对象值的多个选项

这是一个代码:

myapp.controller('myctrl', [
        '$scope',
        function ($scope) {
            $scope.query = {
                Statuses: {
                    Draft: true,
                    Live: true,
                    Pending: true,
                    Archived: false,
                    Deleted: false
                }
            };


        }
    ]);​

和 html

<div ng-controller="myctrl">
<select multiple>
    <option value="Draft" ng:model="query.Statuses['Draft']">Draft</option>
    <option value="Pending" ng:model="query.Statuses.Pending">Pending</option>
    <option value="Live" ng:model="query.Statuses.Live">Live</option>
    <option value="Archived" ng:model="query.Statuses.Archived">Archived</option>
    <option value="Deleted" ng:model="query.Statuses.Deleted">Deleted</option>
</select>

    {{query | json}}
</div>

jsfiddle 上的(非)工作示例​</p>

http://jsfiddle.net/andrejkaurin/h9fgK/

4

4 回答 4

14

使用状态模型($scope.statuses)和 ng-options 来迭代它们:

function MyCtrl($scope) {
    $scope.statuses = [ 'Draft', 'Pending', 'Live', 'Archived', 'Deleted' ];
    $scope.selectedStatuses = [ 'Pending', 'Live' ];
}​

.

<select ng-model="selectedStatuses" multiple ng-options="status for status in statuses">
</select>
于 2012-11-20T19:48:36.263 回答
12

您正在尝试使用多选,例如复选框列表,这有点奇怪。多选输出一个数组。您不能将 ng-model 放在这样的选项标签上,它会继续选择本身。因此,由于 select 将输出一个值数组,因此您需要遍历这些值并更新范围内的节点。

这是一个演示代码的plunk

这是代码:

JS

function inArray(x, arr) {
  for(var i = 0; i < arr.length; i++) {
    if(x === arr[i]) return true;
  }
  return false;
}

app.controller('MainCtrl', function($scope) {
   $scope.query = {
                Statuses: {
                    Draft: true,
                    Live: true,
                    Pending: true,
                    Archived: false,
                    Deleted: false
                }
            };
  $scope.selectionsChanged = function(){
    for(var key in $scope.query.Statuses) {
      $scope.query.Statuses[key] = inArray(key, $scope.selectedValues);
    }
  };
});

HTML

<select multiple ng-model="selectedValues" ng-change="selectionsChanged()">
    <option value="Draft" ng-selected="query.Statuses.Draft">Draft</option>
    <option value="Pending" ng-selected="query.Statuses.Pending">Pending</option>
    <option value="Live" ng-selected="query.Statuses.Live">Live</option>
    <option value="Archived" ng-selected="query.Statuses.Archived">Archived</option>
    <option value="Deleted" ng-selected="query.Statuses.Deleted">Deleted</option>
</select>
<br/>
    {{query | json}}

我希望这会有所帮助。

于 2012-11-20T15:10:12.570 回答
6

需要指出的是,IMO 多选元素是 UI 交互的邪恶。在不记得按住修饰键的情况下触摸任何东西,有些用户不知道,你会失去整个选择。特别糟糕的是,如果有足够多的选项使它们并非全部可见,那么您甚至无法判断何时删除现有选择。多个复选框是表示相同可能选项和当前选择的更好方法。可以使容器的价值可滚动,这实际上类似于具有比大小更多选项的多选。(不是我知道的答案......)

于 2012-12-11T18:07:42.730 回答
6

这是 blesh 解决方案的替代方案

app.controller('MainCtrl', function($scope) {

  $scope.query = {
      Statuses: ["Pending","Live"]
  };
});

并选择

<select multiple ng:model="query.Statuses" >
      <option value="Draft">Draft</option>
      <option value="Pending">Pending</option>
      <option value="Live">Live</option>
      <option value="Archived">Archived</option>
      <option value="Deleted">Deleted</option>
  </select>
  {{query | json}}

工作样本在这里:

http://plnkr.co/edit/bCLnOo

于 2012-11-20T16:36:17.130 回答