如何使用 AngularJS 检索所有选中项目的列表?

How do I retrieve a list of all checked items using AngularJS?

我正在尝试更改 this AngularJS 同步示例的功能 (Using <input type="checkbox">) 到 return 对象数组。

在我的 html 中,我有以下代码使用 ng-repeat 和 AngularJS 演示示例中的一些函数调用:

<div ng-repeat="item in items" class="standard" flex="50">
      <label> <input type="checkbox" ng-checked="exists(item, selected)" /> {{item.name}} </label>
</div>

我还有一个按钮 select 或删除所有复选框条目:

 <label> <input type="checkbox" ng-checked="isChecked()" ng-click="toggleAll()" autofocus ng-model="checkbox" ng-change='delayedSearch(0)'/>
        <span ng-if="!isChecked()">all</span>
        <span ng-if="isChecked()">nothing</span>
  </label> <br><br>

在我的控制器中,我按如下方式初始化数组项:

$scope.items = [{name:'K://',value:'nemo'},{name:'Bugzilla',value:'bugzilla'},{name:'Jira',value:'jira'}];

以及与复选框交互的相应函数:

    $scope.toggle = function (item, list) {
            var idx = list.indexOf(item);
            if (idx > -1) {
                    list.splice(idx, 1);
                    if (list.length == 0){
                            list.push('nothing');
                    }
            }else {
                    var id = list.indexOf('nothing');
                    if (id > -1) {
                            list.splice(id,1);
                    }
                    list.push(item);
            }
    };

    $scope.exists = function (item, list) {
            return list.indexOf(item) > -1;
    };

    $scope.isChecked = function() {
            return $scope.selected.length === $scope.items.length;
    };

    $scope.toggleAll = function() {
            if ($scope.selected.length === $scope.items.length) {
                    $scope.selected = ['nothing'];
            } else if ($scope.selected.length === 0 || $scope.selected.length > 0) {
                    $scope.selected = $scope.items.slice();
            }
    };

目前我的代码return是所有勾选项目的对象列表,例如:

[{"name":"K://","value":"nemo"},{"name":"Bugzilla","value":"bugzilla"},{"name":"Jira","value":"jira"}]

我想获得一个列表,其中包含所有被选中的项目的值:

["nemo","bugzilla","jira"]

您可以像这样映射结果列表:

   var valuesArray =  [{"name":"K://","value":"nemo"},{"name":"Bugzilla","value":"bugzilla"},{"name":"Jira","value":"jira"}].map(function(obj) {
      return obj.value;
    }); // ["nemo","bugzilla","jira"]