离子 - 如何在离子复选框中获取所选项目

时间:2017-02-11 06:40:07

标签: javascript jquery angularjs ionic-framework angularjs-ng-model

我想在Ionic复选框中选择所选项目。因此,当用户选中复选框时,我想知道他/她选择了哪本书。

以下是我的代码段:

int i;
scanf("%d", &i);
scanf("%d", &i); // it remembers*, so it reads the next integer

运行代码时,它看起来像这样:

enter image description here

我想使用ng-model但我不知道如何使用它。有人可以帮助我,因为我是Ionic和AngularJS的新手,谢谢!

更新: 非常抱歉,我没有提供控制器的代码片段,以下是代码:

        <ion-item ng-repeat="book in data.books">

                <div style="margin-left:110px;margin-right:10px;">
                    {{book.name}}
                </div>

                <li class="item item-checkbox  checkbox-balanced">

                    <label class="checkbox">
                        <input type="checkbox">
                    </label>
                </li>

            </ion-item>

1 个答案:

答案 0 :(得分:2)

您可以在每个值中包含 checked 的变量,并将其作为 model 变量绑定到复选框

<强> DEMO

var clubApp = angular.module('clubApp', ['ionic'])
clubApp.controller('ctrlPlayer', ['$scope', function($scope) {
  $scope.books = [{
    "name": "And the goog news is",
    "checked": false
    }, {
    "name": "Girl on the train",
    "checked": false
  }];
 $scope.getselected = function(){
    angular.forEach($scope.books,function(key,value){
        if(key.checked ==true)
        {
          alert(key.name);
        }
    });
 }

}]);
<!DOCTYPE html>
<html ng-app='clubApp'>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" type="text/css" href="//code.ionicframework.com/1.0.0-beta.11/css/ionic.min.css">
  <script src="//code.ionicframework.com/1.0.0-beta.9/js/ionic.bundle.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.min.js"></script>

  <script src="script.js"></script>
</head>
<body ng-controller="ctrlPlayer">
 <ion-list>
  <ion-checkbox ng-model=value.checked   ng-repeat="(key, value) in books">
  <span>{{value.name}}</span>   
  </ion-checkbox>
</ion-list>
 <button ng-click="getselected()" class="button button-positive">
            get selected
 </button>
  <h1>{{selectedval}}</h1>
</body>

</html>