如何使用Angular Js检查存储在DB中的现有逗号seaprated值的复选框值

时间:2018-06-05 07:54:44

标签: javascript angularjs

我将设施复选框选项显示为json数组的ng-repeat,并以逗号分隔的ID存储到数据库中,例如" 1,3,7"作为单个字符串,但当我需要编辑设施复选框时,我无法显示已检查的现有选项,因为我从db获得现有值1,3,7作为单个字符串我将如何检查此字符串中的ng-repeat中的选项?



<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.amenities=[{id:1,value:"Wifi"},
                  {id:2,value:"AC"},
                  {id:3,value:"Creditcard"},
                  {id:4,value:"24x7"},
                  {id:5,value:"Parking"},
                  {id:6,value:"Free delivery"},
                  {id:7,value:"No Smoking"}
                 ];

     //For example  assume that this is existing amenities from database
     $scope.existamenities="1,3,7";            
});

</script>

<htmL>
<div ng-app="myApp" ng-controller="myCtrl">

<div ng-repeat="amn in amenities">	
  <label>{{amn.value}}
<input type="checkbox"  name="amenities"   ng-value="amn.id">
</label>
</div>



</div>

</htmL>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

我相信要检查它们,您需要将ng-model设置为true或false。只需创建另一个数组并相应地填充它。

您可以选择使用包含true / false值的新属性附加现有数组,或创建从原始数组映射的新数组。然后只需将默认false值切换为true ,如果列出了他们的ID,您可以通过拆分字符串来获取。

这是一个小型演示:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.amenities = [
    {id:1,value:"Wifi"},
    {id:2,value:"AC"},
    {id:3,value:"Creditcard"},
    {id:4,value:"24x7"},
    {id:5,value:"Parking"},
    {id:6,value:"Free delivery"},
    {id:7,value:"No Smoking"}
  ];

  //For example  assume that this is existing amenities from database
  $scope.existamenities = "1,3,7";

  var a = $scope.existamenities.split(",").map(x => Number(x))
  var b = $scope.amenities.map((x) => {
    return {
      "id": x.id,
      "val": false
    }
  })
  for (var i = 0; i < b.length; i++) {
    for (var j = 0; j < a.length; j++) {
      if (b[i].id == a[j]) {
        b[i].val = true
      }
    }
  }
  $scope.model = b;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

  <div ng-repeat="amn in amenities">
    <label>{{amn.value}}
<input type="checkbox"  name="amenities"  ng-model="model[$index].val" ng-value="amn.id">
</label>
  </div>

答案 1 :(得分:1)

您需要在每个复选框上使用ng-model,以便能够控制它们是否已经检查过......并且能够分辨出哪些在以后检查过,例如表格提交。

为了做到这一点:

  • 设施对象应具有checked属性
  • ng-repeat中的复选框应将ng-model绑定到checked属性

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.amenities=[{id:1,value:"Wifi", checked: false},
                  {id:2,value:"AC", checked: false},
                  {id:3,value:"Creditcard", checked: false},
                  {id:4,value:"24x7", checked: false},
                  {id:5,value:"Parking", checked: false},
                  {id:6,value:"Free delivery", checked: false},
                  {id:7,value:"No Smoking", checked: false}
                 ];

     //For example  assume that this is existing amenities from database
     $scope.existamenities="1,3,7";            

     var arrayOfExistingAmenities = $scope.existamenities.split(",");
     arrayOfExistingAmenities.forEach(function(existingAmenity) {
        $scope.amenities.forEach(function(amenity) {
            if (amenity.id == parseInt(existingAmenity)) {
                amenity.checked = true;
            }
        });
     });


});

</script>

<htmL>
<div ng-app="myApp" ng-controller="myCtrl">

<div ng-repeat="amn in amenities">	
  <label>{{amn.value}}
<input type="checkbox"  name="amenities" ng-model="amn.checked"  ng-value="amn.id">
</label>
</div>



</div>

</htmL>
&#13;
&#13;
&#13;