删除"已检查"来自json

时间:2014-08-25 16:55:30

标签: javascript json angularjs

我已关注此帖:How do I bind to list of checkbox values with AngularJS?

从复选框列表中检索数据并将其作为Json传递。

我的代码是:

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

这是我的控制器代码:

$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);
    })
};

问题是,在最后的json中,我得到的是这样的东西:

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

问题是: Web服务无法识别在此过程中添加的“已检查”。我该如何删除它?

1 个答案:

答案 0 :(得分:1)

您可以使用delete关键字从对象中删除属性。例如,给定以下对象:

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

删除checked属性:

delete b.checked;

这导致以下对象:

{ Name:"Name1", Id:51 }

小提琴:http://jsfiddle.net/t0upqxvt/