如何在angularjs中获取复选框值? [英] How to get checkbox value in angularjs?
本文介绍了如何在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;
});
}
这篇关于如何在angularjs中获取复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文