0

I have followed this post: How do I bind to list of checkbox values with AngularJS?

To retrieve data from checkbox list and pass it as a Json.

My code is this:

<span ng-repeat="b in neighborhood">
                    <input id="{{b.Id}}" class="checkcheck" type="checkbox" ng-model="b.checked" ng-change="selection.indexOf(b) > -1"
                           ng-click="toggleSelection(b)">
                    {{ b.Name }}

And this my controller code:

$scope.toggleSelection = function toggleSelection(b) {
    var idx = $scope.selection.indexOf(b);

    if (idx > -1) {
        $scope.selection.splice(idx, 1);
    }
    else {
        $scope.selection.push(b);
    }
    console.log($scope.selection);

    ProgramsWS.GetByNeighborhood({ neighborhoodList: $scope.selection }, function (resp) {
        console.log(resp);
    })
};

The thing is that in the final json, I'm getting something like this:

 

0       b { Name="Name1", Id=51, checked=true, more...}
1       b { Name="Name2", Id=43, checked=true, more...}

The problems is: Web Service doesn't recognize the "checked" that has been added there in all this process. How can I remove it?

Community
  • 1
  • 1
SrAxi
  • 19,787
  • 11
  • 46
  • 65

1 Answers1

1

You can delete properties from objects using the delete keyword. For example, given the following object:

var b = { Name:"Name1", Id:51, checked:true };

Delete the checked property:

delete b.checked;

This results in the following object:

{ Name:"Name1", Id:51 }

Fiddle: http://jsfiddle.net/t0upqxvt/

forgivenson
  • 4,394
  • 2
  • 19
  • 28
  • Nice, but is there a way to select the Id instead of deleting all the rest? So I could have something like: [{Id:51}, {Id: 43}] – SrAxi Aug 25 '14 at 17:11
  • Is not deleting the 'checked'. Could you add a fiddle or something? Thank you for your quick answer. – SrAxi Aug 25 '14 at 17:14
  • I added a fiddle using the example I used in my answer. Can you show me what changes you made to your code? – forgivenson Aug 25 '14 at 17:21
  • At the end I managed to find a solution according to my problem, without deleting "checked". But as what I asked in first place was how to delete it, I set your answer as answer for the problem and give you a +1. Thank you for your help. ;) – SrAxi Aug 26 '14 at 08:20