我使用以下代码显示我的复选框列表:
<ion-checkbox ng-repeat="item in listPref"
ng-model="item.checked" ng-checked="item.checked">
{{ item.text }}
</ion-checkbox>
这是我的列表偏好:
$scope.listPref = [
{text: 'Cinema'},
{text: 'Sport'},
{text: 'It'} ,
{text: 'Music'},
{text: 'Theater'},
{text: 'Conference'}];
我尝试使用此代码来获取所有选定项目的文本
for(var i =0 ; i <$scope.listPref.length ; i++){
console.log($scope.listPref[i].checked.text);
}
我收到消息undefined
Cannot read property 'text' of undefined
at Scope.$scope.pref
在我的控制台中。
有人能帮助我吗。
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(内部控制器):
$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()
将选中的复选框的值存储在数组中,然后打印该数组。您可以对这些值执行任何您喜欢的操作,此示例将它们存储在一个数组中并打印到控制台,以便可以轻松地看到发生的情况。
如果您有任何疑问,请评论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)