从json数组angularjs中获取选定的离子复选框

时间:2016-06-01 04:34:58

标签: javascript angularjs json ionic-framework

我有以下数组:

$scope.profile.preferencias = [7,5,3,8,4];

修改 我的类别数组如下所示:

[
  {
    "id": 2,
    "descricao": "Entomologia",
    "dataCriacao": "0001-01-01T00:00:00",
    "isActive": true,
    "isFixed": true
  },
  {
    "id": 3,
    "descricao": "Fisiologia",
    "dataCriacao": "0001-01-01T00:00:00",
    "isActive": true,
    "isFixed": true
  }
]

我的离子复选框如下所示:

<ion-checkbox ng-repeat="category in categories | toArray | orderBy:'descricao'" name="group" ng-model="profile.preferencias[category.id]">{{category.descricao}}</ion-checkbox>

选择的输出是:

{
   "7": true,
   "5": false,
   "3": true
}

我如何只将号码发送到我的json,所以它会像上面的$ scope一样保存,当重新加载页面时,复选框会重新检查?

谢谢!

1 个答案:

答案 0 :(得分:1)

我在我的应用程序中使用相同的东西。

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

和JSON数据

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

选择上面的$ scope变量是默认复选框。如果您不想要默认复选框,则选择&#34;选择&#34;:false&#34;

谢谢,