我使用以下代码显示我的复选框列表:
<ion-checkbox ng-repeat="item in listPref"
ng-model="item.checked" ng-checked="item.checked">
{{ item.text }}
</ion-checkbox>
Run Code Online (Sandbox Code Playgroud)
这是我的listPref:
$scope.listPref = [
{text: 'Cinema'},
{text: 'Sport'},
{text: 'It'} ,
{text: 'Music'},
{text: 'Theater'},
{text: 'Conference'}];
Run Code Online (Sandbox Code Playgroud)
我尝试使用此代码来获取所有选定项目的文本
for(var i =0 ; i <$scope.listPref.length ; i++){
console.log($scope.listPref[i].checked.text);
}
Run Code Online (Sandbox Code Playgroud)
我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>
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
你可以用价值做任何你想做的事.print()只打印当前选中或取消选中的所有值的对象(true/false).save()将选中复选框的值存储在数组中,然后打印数组.您可以使用值执行任何您喜欢的操作,此示例将它们存储在一个数组中并打印到控制台,以便可以轻松看到正在发生的事情.
如果您有任何疑问,请评论.
| 归档时间: |
|
| 查看次数: |
11855 次 |
| 最近记录: |