6

In my app I got 7 checkboxes. I want to get the value of the selected checkbox and store into an object. Ff it is deselected I want to remove it in the object.

HTML

<span ng-repeat="days in selectDays">
    <input type="checkbox" id="{{days}}" ng-model="daysSelected"/>
    <label for="{{days}}">{{days}}</label>
</span>

Controller

$scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$scope.selectedList = {}; //this is the object to store the selected checkbox values
2

3 Answers 3

11

The following code is a simple approach -> check this plunker. This example delivers you a very simple KISS principle handling for mulitple autogenerated checkboxes in AngularJS.

Template

<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>

Scopes

//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);
        }
    });
};
Sign up to request clarification or add additional context in comments.

6 Comments

Great +1, but its helpful to avoid using console in you plunker, try adding: <p>{{selectedList|json}}</p> to your plunker.
You also need to check "selected" variable in the loop. It will get listed if you once checked and un-check the same value.
Thanks @lin, Very simple and easy to understand.
How can check the checkbox automatically ? eg. If i will get the data "SUN" from backend then i have to check the "Sun" checkbox. if i will get multiple data like "SUN, MON", then i need to check the respective checkboxes automatically. In this case how to do this ?
@Kishan open a new question please.
|
0
    $scope.checkFun= function(data) {


                    for(var i in data){                                                                              
                       if(data[i].SELECTED=='Y'){ 
                          $scope.selectedList.push(data[i]);  
                         }
                        if(data[i].SELECTED=='N'){  
                          $scope.selectedList.splice(data[i]);
                         }
    }

<input type="checkbox" id="{{days}}" ng-model="selectDays.SELECTED" ng-true-value="'Y'" ng-false-value="'N'" ng-click="checkFun(selectDays)">

3 Comments

The answer is not simple as the question.
@FaouziOudouh did i missed any thing ?
Nothing but your answer added unwanted complexity the job.
0

I have created a jsfiddle, it uses ngChange directive from angular whenever the checkbox is clicked a function is called. The code looks something like this

<div ng-controller="MyCtrl">
<span ng-repeat="day in selectDays">
    <input type="checkbox" id="{{day}}" ng-model="daysSelected" ng-change= "change(day)"/>
    <label for="{{day}}">{{day}}</label>
</span>
<span ng-repeat="day in selectedList">
<div>
{{day}}
</div>

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$scope.selectedList = []; 
$scope.change = function(day) {
var indexOfDay = $scope.selectedList.indexOf(day); 
    if(indexOfDay === -1) {
        $scope.selectedList.push(day)
    } else {
        $scope.selectedList.splice(indexOfDay, 1)
    }
 }
}

1 Comment

How can check the checkbox automatically ? eg. If i will get the data "SUN" from backend then i have to check the "Sun" checkbox. if i will get multiple data like "SUN, MON", then i need to check the respective checkboxes automatically. In this case how to do this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.