angularjs md checkbox 获取选中的值

angularjs md checkbox get the checked values

<md-checkbox ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-model="ctrl.primaryProgramStudiesSelected[primaryPrograms.id]">
  {{primaryPrograms.name}}
</md-checkbox>

选中的复选框:

{{ctrl.primaryProgramStudiesSelected | json}}

我得到的输出:

Selected Checbox : 
[null,true,true,true,null,true,null,true,null,true,null,null,true] 

如何获取检查值列表。

您可以使用 filter 方法,该方法接受 callback 方法作为参数。

filter() 方法创建一个新的 array,其中所有元素都通过 provided(callback) 函数实现的测试。

var array=[null,true,true,true,null,true,null,true,null,true,null,null,true];
ctrl.primaryProgramStudies.filter(function(item,index){
    return array[index]==true;
});

简短示例

var ctrl = {};

ctrl.primaryProgramStudies = [{
    "name": "program0"
  },
  {
    "name": "program1"
  },
  {
    "name": "program2"
  },
  {
    "name": "program3"
  },
  {
    "name": "program4"
  },
  {
    "name": "program5"
  },
  {
    "name": "program6"
  },
  {
    "name": "program7"
  },
  {
    "name": "program8",
  },
  {
    "name": "program9",
  },
  {
    "name": "program10",
  },
  {
    "name": "program11",
  },
  {
    "name": "program12"
  }
]

ctrl.primaryProgramStudiesSelected =[null,true,true,true,null,true,null,true,null,true,null,null,true];
var result=ctrl.primaryProgramStudies.filter(function(item,index){
     return ctrl.primaryProgramStudiesSelected[index]==true;
});

console.log(result)

可以根据ctrl.primaryProgramStudiesSelected上的相同索引是否有true

过滤原数组ctrl.primaryProgramStudies

var ctrl = {};

ctrl.primaryProgramStudies = [{
    "name": "test0",
    "id": 0
  },
  {
    "name": "test1",
    "id": 1
  },
  {
    "name": "test2",
    "id": 2
  },
  {
    "name": "test3",
    "id": 3
  },
  {
    "name": "test4",
    "id": 4
  },
  {
    "name": "test5",
    "id": 5
  },
  {
    "name": "test6",
    "id": 6
  },
  {
    "name": "test7",
    "id": 7
  },
  {
    "name": "test8",
    "id": 8
  },
  {
    "name": "test9",
    "id": 9
  },
  {
    "name": "test10",
    "id": 10
  },
  {
    "name": "test11",
    "id": 11
  },
  {
    "name": "test12",
    "id": 12
  }
]

ctrl.primaryProgramStudiesSelected = [null, true, true, true, null, true, null, true, null, true, null, null, true]

ctrl.selectedValues = ctrl.primaryProgramStudies.filter(function(obj, index) {
  return ctrl.primaryProgramStudiesSelected[index] === true
})

console.log(ctrl.selectedValues)