AngularJS复选框未选中的值

时间:2019-01-04 08:05:35

标签: angularjs checkbox angularjs-scope selected

您好,这个问题已经提出,但是不能解决我的问题。

我正在将复选框值传递给控制器​​,但是我无法确定选择了哪个。

这是我的代码

HTML:

<div class="col-sm-3" ng-repeat="days in formData.businessDays">
   <input type="checkbox" ng-model="business_days.Selected" ng-change=""><span>  {{days.name}}</span>
</div>

JS:

$scope.addData = function(form)
{
    $scope.formData.days_id = [];
    angular.forEach($scope.formData.businessDays, function(businessDays){
       //console.log(businessDays);
        if (businessDays.Selected) {
            $scope.formData.days_id.push(businessDays.value);
        }
    })
}

//this days data values
$scope.formData.businessDays = [{
                    name: "Monday",
                    value: "M",
                  }, {
                    name: "Tuesday",
                    value: "T",
                  }, {
                    name: "Wednesday",
                    value: "W",
                  }, {
                    name: "Thursday",
                    value: "Th",
                  }, {
                    name: "Friday",
                    value: "F",
                  }, {
                    name: "Saturday",
                    value: "S",
                  }, {
                    name: "Sunday",
                    value: "Su",
                  }, ];

这是我的控制台映像 enter image description here

  

我无法确定所选的值是哪个?

3 个答案:

答案 0 :(得分:4)

 <div class="col-sm-3" ng-repeat="days in formdata.businessDays">
    <input type="checkbox" ng-model="days.selected" ng-click="selected(days)"> 
    <span> {{days.name}}</span>
 </div>

和控制器

 $scope.selected = function(d){
    console.log(d);

}

$scope.formdata = {};
$scope.formdata.businessDays = [{
    name: "Monday",
    value: "M",
}, {
    name: "Tuesday",
    value: "T",
}, {
    name: "Wednesday",
    value: "W",
}, {
    name: "Thursday",
    value: "Th",
}, {
    name: "Friday",
    value: "F",
}, {
    name: "Saturday",
    value: "S",
}, {
    name: "Sunday",
    value: "Su",
},];

答案 1 :(得分:1)

您必须为selected中的每个object提供一个属性$scope.formData.businessDays,以标识是否已选择该属性,例如:-

$scope.selectedItems = [];
  $scope.formData = {};
  $scope.formData.businessDays = [{
    name: "Monday",
    value: "M",
    selected: false
  }, {
    name: "Tuesday",
    value: "T",
    selected: false
  }, {
    name: "Wednesday",
    value: "W",
    selected: false
  }, {
    name: "Thursday",
    value: "Th",
    selected: false
  }, {
    name: "Friday",
    value: "F",
    selected: false
  }, {
    name: "Saturday",
    value: "S",
    selected: false
  }, {
    name: "Sunday",
    value: "Su",
    selected: false
  }];
  $scope.changed = function(value) {
    if (value.selected) {
      $scope.selectedItems.push(value.name);
    } else {
      var theIndex = $scope.selectedItems.indexOf(value.name);
      if (theIndex != -1) {
        $scope.selectedItems.splice(theIndex, 1);
      }
    }
  }

并订阅change的{​​{1}}事件以使媒体资源保持最新状态

checkboxes

答案 2 :(得分:0)

<div class="col-sm-3" ng-repeat="days in formData.businessDays">
   <input type="checkbox" ng-model="days.Selected" ng-change=")"><span>  {{days.name}}</span>
</div>


$scope.addData = function(form)
{
    $scope.formData.days_id = [];
    angular.forEach($scope.formData.businessDays, function(businessDays){
       //console.log(businessDays);
        if (businessDays.Selected) {
            $scope.formData.days_id.push(businessDays.value);
        }
    })
}