Reputation: 2551
I'm trying to do something very simple with checkboxes and Angular 1.x. I want to pre-select some checkboxes on an edit page and then after some selections are made to save the data to a backend.
I have created this demo:
http://jsfiddle.net/roL38ctk/3/
I'm able to pre-select the checkboxes but I struggle to see how the selected items or of any use and could ever be saved to a database, for example after checking and un-checking the options my ng-model might look like this:
{"300":false,"400":true}
When saving I need to know the ids which have been selected, I have no need or desire to know the state of the checkboxes. Something like:
{300,305,310}
To me it seems that Angular doesn't handle checkboxes very well without having to hack objects together to extract and massage the data ready for saving.
As a guide I have used http://embed.plnkr.co/g0NMG4rmF4uwzoG2uZhf/preview which I believe is reputable source and claims to be the Angular way? I'm disbelieving that something some elementary can be so taxing when a PHP framework manages this effortlessly. Where am I going wrong?
Upvotes: 0
Views: 60
Reputation: 3242
Just put this code in your submit function. You will get values as your expectation:
var arryList=[];
angular.forEach($scope.selection.ids, function (val, key) {
if(val==true){
var vd = key;
arryList.push(vd);
}
});
alert(arryList);
Or you can use angularjs material checkboxs Check here.
It's third demo already satisfy your requirement.
Upvotes: 4