繁体   English   中英

如何使用Angular.js获取多选数据并在列表的每个项目中添加复选框?

[英]How to get multi select data and add check box in each item of the list using Angular.js?

我需要在每个项目中添加复选框并使用 Angular.js 获取多选值。 我的代码如下。

<body ng-controller="speshController">
  <h1>Hello</h1>
  <div class="col-md-6">
    <div class="input-group bmargindiv1 col-md-12">
      <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Child Business Name :</span>
      <select class="form-control" multiple="multiple" id="restau" ng-model="restaurant" ng-options="qua.name for qua in listOfRestaurant  track by qua.value" ng-change="getDayFromSpecial('restau');">
      </select>
    </div>
  </div>
  <button type="button" id="btn" ng-click="getValue();">GET</button>
</body>

这里我有一个下拉列表,我需要在下拉列表中的每个项目之前添加复选框。 假设用户在单击GET按钮时选择了多个项目,我需要获取这些选定的数据。 我的控制器端代码如下。

app.controller('speshController',function($scope,$http){
  $scope.data=[{
    'value':1,
    'name':'aaa'
  },{
    'value':2,
    'name':'bvc'
  },{
    'value':3,
    'name':'rtcv'
  },{
    'value':4,
    'name':'uytg'
  }
  ]
  $scope.listOfRestaurant=[];
  angular.forEach($scope.data,function(obj){
    var data={'name':obj.name,'value':obj.value};
        $scope.listOfRestaurant.push(data);
  })
  $scope.getValue=function(){

  }

})

我需要在控制台中显示那些选定的数据。 我的完整代码在这里

多选的 ng-model 将为您提供所选项目作为数组

 $scope.getValue=function(){
   console.log($scope.restaurant) 
 }

https://plnkr.co/edit/cqwbxRnQQAVrSm55nI6P?p=preview

在我看来,最好使用带有标签的复选框而不是多选下拉列表。

<label ng-repeat="qua in listOfRestaurant track by qua.value">
    <input type="checkbox" ng-model="qua.selected"/>
    {{qua.name}}
</label>

您可以在Plunker 中进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM