如何获得离子复选框的价值 [英] how to get value of ion-checkbox

查看:73
本文介绍了如何获得离子复选框的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码显示我的复选框列表:

I display my checkbox list using this code :

 <ion-checkbox ng-repeat="item in listPref"
   ng-model="item.checked" ng-checked="item.checked">
   {{ item.text }}
 </ion-checkbox>

这是我的listPref:

This is my listPref:

 $scope.listPref = [
    {text: 'Cinema'},
    {text: 'Sport'},
    {text: 'It'} ,
    {text: 'Music'},
    {text: 'Theater'},
    {text: 'Conference'}];

我尝试使用此代码获取所有选定项目的文本

I try this code to get the text of all selected item

 for(var i =0 ; i <$scope.listPref.length ; i++){
console.log($scope.listPref[i].checked.text); 
 } 

我收到消息 undefined 无法在我的控制台中读取Scope。$ scope.pref 中未定义
的属性'text'。
请有人帮帮我。

I get the message undefined Cannot read property 'text' of undefined at Scope.$scope.pref in my console. Can someone help me please.

推荐答案

HTML:

<ion-content>
   <ion-checkbox ng-repeat="item in listPref"
       ng-model="checkItems[item.text]" ng-change="print()">
       {{ item.text }}
   </ion-checkbox>
   <button type="button" name="button" ng-click="save()">Save</button>
</ion-content>

JS(控制器内部):

JS (inside controller):

$scope.listPref = [
    {text: 'Cinema'},
    {text: 'Sport'},
    {text: 'It'} ,
    {text: 'Music'},
    {text: 'Theater'},
    {text: 'Conference'}
];

$scope.checkItems = { }

$scope.print = function() {
    console.log($scope.checkItems);
}

$scope.save = function() {
    var array = [];
    for(i in $scope.checkItems) {
        console.log($scope.checkItems[i]);
        if($scope.checkItems[i] == true) {
            array.push(i);
        }
    }
    console.log(array);
}

您可以使用值执行任何操作。 print()只打印当前选中或取消选中的所有值的对象(true / false)。 save()将已选中复选框的值存储在数组中,然后打印数组。你可以用值做任何你喜欢的事情,这个例子将它们存储在一个数组中并打印到控制台,这样可以很容易地看到发生的事情。

You can do whatever you want with the values. print() just prints the object of all the values currently checked or unchecked (true/false). save() stores the value of the checked checkbox inside an array and then prints the array. You can do whatever you like with the values, this example stores them in an array and prints to the console so what is happening can easily be seen.

如果你有评论任何问题。

Comment if you have any questions.

这篇关于如何获得离子复选框的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆