Vishva G
Vishva G

Reputation: 399

How to have multiple checkboxes use the same ng-model?

I am using Ionic framework for mobile app development. I set multiple checkboxes using the same ng-model, so when I click one checkbox all three checkboxes should be clicked. But the values are not stored. But I want normal HTML checkboxes (i.e) values stored in the same field in my db separated by commas.

<label class="item item-input item-floating-label">Gender</label>
<input type="checkbox" ng-model="data.days" value="sunday">sunday
<input type="checkbox" ng-model="data.days" value="monday">monday

Upvotes: 0

Views: 3404

Answers (2)

Anurag Pandey
Anurag Pandey

Reputation: 744

i am getting day's in single ng-model.

Sample Code

<div ng-repeat="day in days1" class="week_days">
        <ion-checkbox ng-model="day.select" style="border:none">{{day.name}}</ion-checkbox>
    </div>

controller side

$scope.days = [{
            "id": 1,
            "name": "Mon",
            "select": true
        }, {
            "id": 2,
            "name": "Tue",
            "select": true
        }, {
            "id": 3,
            "name": "Wed",
            "select": true
        }, {
            "id": 4,
            "name": "Thu",
            "select": true
        }, {
            "id": 5,
            "name": "Fri",
            "select": true
        }, {
            "id": 6,
            "name": "Sat",
            "select": true
        }, {
            "id": 0,
            "name": "Sun",
            "select": true
        }];

above code is default all checkbox is selected when click on checkbox then auto modified days object.

Upvotes: 1

Massimo Rosin
Massimo Rosin

Reputation: 163

Create an object and use it as your ng-model.

Check this plunker:

https://plnkr.co/edit/YHJDbU24BIaSrmlDTrZy?p=preview

I think this is what you want.

Upvotes: 0

Related Questions