我怎样才能在AngularJS复选框全部选中的对象? [英] How can I get all the selected objects of Checkboxes in AngularJS?

查看:133
本文介绍了我怎样才能在AngularJS复选框全部选中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AngularJS的复选框全部选中的对象。

I want to get all the selected objects of the checkboxes using AngularJS.

下面是我的情况。

我的view.tpl.html

<tr ng-repeat="item in itemList">
<td>
<input type="checkbox" ng-click="clickedItem(item.id)" 
       ng-model="model.controller.object"
       {{item.name}}
</td>

我的控制器

  $scope.itemList = [
{
  id:"1",
  name:"first item"
},
{
  id:"2",
  title:"second item"
},
{
  id:"3",
  title:"third item"
}
];

   $scope.selection = [];
    $scope.clickedItem = function(itemId) {
        var idx = $scope.selection.indexOf(itemId);
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        }

        // is newly selected
        else {
            var obj = selectedItem(itemId);
            $scope.selection.push(obj);
        }
    };

    function selectedItem(itemId) {
        for (var i = 0; i < $scope.itemList.length; i++) {
            if ($scope.itemList[i].id === itemId) {
                return  $scope.itemList[i];
            }
        }
    }

下面我将得到所有在 $ scope.selection 选定的项目。我怎样才能得到它 NG-模型

Here I will get all the selected items in $scope.selection. How can I get it to ng-model?

是否有可能做这样 NG-模式=model.controller.object =选择因为我需要选择的 $ scope.selection 分配到 model.controller.object

Is it possible to do like ng-model="model.controller.object = selection" since I need the selected $scope.selection to be assigned to model.controller.object

推荐答案

如果我理解正确的话,你要创建一个复选框,并动态地将其绑定到从列表中的项目(S),如果是这样,这是我怎么会去这样做:

If I understand correctly you want to create a checkbox and dynamically bind it to an item(s) from a list, if so this is how I would go about doing it:

$scope.modelContainer=[];
angular.forEach($scope.itemList,function(item){
  $scope.modelContainer.push({item:item,checked:false });

});

HTML

<div ng-repeat="item in itemList">
{{item.name}} 
<input type="checkbox" ng-click="selected(item.id)"   ng-model="modelContainer[$index].checked"     />
</div>

请参阅普拉克。在控制台中看到的模型容器变化,只要你选中一个复选框。

See plunk. See the model container change in the console whenever you check a checkbox.

这篇关于我怎样才能在AngularJS复选框全部选中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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