簡體   English   中英

如何使用AngularJS檢索所有選中項目的列表?

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

我正在嘗試更改 AngularJS示例的功能, 示例進行同步( Using <input type="checkbox"> )以返回對象數組。

在我的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>

我還有一個按鈕可以選擇或刪除所有復選框條目:

 <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();
            }
    };

當前,我的代碼返回所有選中項的對象列表,例如:

[{"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"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM