Nisham Mahsin
Nisham Mahsin

Reputation: 1399

Set checkboxes as checked using angularjs

I have some checkboxes and their value is in database are 'Y' or 'N' as enum.for making updation i have to review all the checkboxes.how to view checkboxes as checked. this is code for checkboxes

<label class="radio-inline">
  <input type="checkbox"  ng-model="newItem.have_voter_id"  value="have_voter_id" /><?php echo $this->lang->line('label_voter_id'); ?>
</label>
<label class="radio-inline">
  <input type="checkbox"   ng-model="newItem.have_passport" value="passport" /> <?php echo $this->lang->line('label_passport'); ?>
 </label>

and this is the function for view and updation

 $scope.edit = function(id,family_id) {

    $scope.action = 'update';
    FamilyMem.get({id:id,family_id:family_id}, function (response) { // success            
        $scope.newItem = response.data; // store result to variable            
          $("#myModal").modal('show');
        }, function (error) { // ajax loading error
            Data.errorMsg(); // display error notification
            //$scope.edit = false;
        });
  };

and edit button

  <a href="" class="btn btn-magenta btn-sm" ng-click="edit(family_member.id,family_member.family_id)">

Upvotes: 7

Views: 32816

Answers (3)

Shahid Hussain Abbasi
Shahid Hussain Abbasi

Reputation: 2692

The below link helped me a lot.

You need to initialize model and then if model value match with value it will be get selected.

Upvotes: 0

Anshad Vattapoyil
Anshad Vattapoyil

Reputation: 23463

Use ng-checked for check-boxes,

<label class="radio-inline">
  <input type="checkbox"  ng-model="newItem.have_voter_id"  value="have_voter_id" ng-checked="newItem.have_voter_id=='Y'"/><?php echo $this->lang->line('label_voter_id'); ?>
</label>

<label class="radio-inline">
  <input type="checkbox"   ng-model="newItem.have_passport" value="passport" ng-checked="newItem.have_passport=='Y'"/> <?php echo $this->lang->line('label_passport'); ?>
</label>

Upvotes: 19

Md Hasan Ibrahim
Md Hasan Ibrahim

Reputation: 1898

The model newItem.have_voter_id and newItem.have_passport is set to true or false according to your action on the checkbox. If the checkbox of 'voter_id' is checked then the value of the model newItem.have_voter_id will be updated to true and if unchecked the value will be updated to false. And the same goes for the other checkbox.

Upvotes: 0

Related Questions