如何在angularjs中获取复选框值? [英] How to get checkbox value in angularjs?

查看:27
本文介绍了如何在angularjs中获取复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ng-view 中有一个 10(或 n)复选框.现在我想问这里检查复选框是否被选中以及如果选中获取复选框的值的最有效或最简单的方法是什么.

I have a 10(or n)checkbox in my ng-view. Now I would like to ask here that What is the most efficient or simple way to check if checkbox is checked and if checked get the value of checkbox.

<ul>
  <li ng-repeat="album in albums">
   <input type="checkbox" ng-model="folder.Selected" value={{album.name}} />
   </li>
  </ul>

我有 ng-controller(getAlbumsCtrl),因为我有一个数组,我想在其中将所有选中的专辑名称推送到 albumNameArray

I have ng-controller(getAlbumsCtrl), in that I have an array in which I want to push all the checkedbox albums names into albumNameArray

推荐答案

您可以遍历数组以找出哪个被选中并推送到名称数组中.

You can loop over the array to find out which is selected and push into the name array.

<ul>
  <li ng-repeat="album in albums">
    <input type="checkbox" ng-model="album.selected" value={{album.name}} />
  </li>
</ul>

$scope.save = function(){
  $scope.albumNameArray = [];
  angular.forEach($scope.albums, function(album){
    if (!!album.selected) $scope.albumNameArray.push(album.name);
  })
}

// alternatively you can use Array.prototype.filter on newer browsers (IE 9+)
$scope.save = function(){
  $scope.albumNameArray = $scope.albums.filter(function(album){
    return album.selected;
  });
}

jsbin

这篇关于如何在angularjs中获取复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆