如何在AngularJS中获取Checkbox的所有选定对象?

 世卍界创意驿站肀 发布于 2023-01-30 18:29

我想使用AngularJS获取复选框的所有选定对象.

以下是我的代码

我的view.tpl.html





我的控制器

  $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-model

是否可以这样做,ng-model="model.controller.object = selection" 因为我需要$scope.selection分配选定的model.controller.object

1 个回答
  • 如果我理解正确你想要创建一个复选框并动态地将它绑定到列表中的项目,如果是这样,我将这样做:

    $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>
    

    见插件.每当您选中复选框时,请在控制台中查看模型容器更改.

    2023-01-30 18:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有