如何在AngularJS中获取多个复选框的值?

Kis*_*han 5 javascript angularjs

在我的应用程序中,我有7个复选框.我想获取所选复选框的值并存储到对象中.如果取消选择,我想在对象中删除它.

HTML

<span ng-repeat="days in selectDays">
    <input type="checkbox" id="{{days}}" ng-model="daysSelected"/>
    <label for="{{days}}">{{days}}</label>
</span>
Run Code Online (Sandbox Code Playgroud)

调节器

$scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$scope.selectedList = {}; //this is the object to store the selected checkbox values
Run Code Online (Sandbox Code Playgroud)

lin*_*lin 9

以下代码是一个简单的方法 - >检查这个plunker.这个例子为AngularJS中的多个自动生成复选框提供了一个非常简单的KISS原理处理.

模板

<span ng-repeat="day in selectDays">
    <input type="checkbox" id="{{day}}" ng-model="selectedList[day]"/>
    <label for="{{day}}">{{day}}</label>
</span>
<button ng-click="submit()">Submit</button>
Run Code Online (Sandbox Code Playgroud)

领域

//default states
$scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$scope.selectedList = {};

/**
 * Action
 */
$scope.submit = function () {
    angular.forEach($scope.selectedList, function (selected, day) {
        if (selected) {
           console.log(day);
        }
    });
};
Run Code Online (Sandbox Code Playgroud)