我正在尝试更改同步(Using <input type="checkbox">
)的this AngularJS示例函数以返回对象数组。
在我的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"]
答案 0 :(得分:1)
您可以像这样映射结果列表:
var valuesArray = [{"name":"K://","value":"nemo"},{"name":"Bugzilla","value":"bugzilla"},{"name":"Jira","value":"jira"}].map(function(obj) {
return obj.value;
}); // ["nemo","bugzilla","jira"]