samedi 4 août 2018

angularjs checkbox Incorrect response in console

I have two criteria: 1) Only allow one of two boxes selected at one time. 2) Capture the name of the box that is selected.

However, when I print out the list of checkbox objects they are correct, but when I check in the console they are not correct. For example,

HTML:

<div ng-repeat="treatment in treatment_list">
 <input type="checkbox" value="" ng-model="treatment.checked" ng-click="updateTreatment($index, treatment_list); checkedTreatment(treatment_list)">
 <label>Treatment </label></input><br>
</div>


Controller:

$scope.treatment_list = [
    {
      name: 'a',
      checked: false
    }, {
      name: 'b',
      checked: false
    }
  ];

$scope.updateTreatment = function(position, treatment_list) {
    console.log(treatment_list);
    angular.forEach(treatment_list, function(treatment, index) {
      console.log(treatment.name, treatment.checked);
      if (position != index) {
        treatment.checked = false;
      }
    });
  };

$scope.$watch('treatment.checked', function (treatment) {
    console.log(treatment);
  });

Plunker: https://plnkr.co/edit/Hkb4IeKxi0TRqHRJA4JN?p=preview




Aucun commentaire:

Enregistrer un commentaire