簡體   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