Prashobh
Prashobh

Reputation: 9542

How to make a checkbox selected default?

<span ng-repeat="location in model.locations ">
    <input type="checkbox" ng-disabled="isDisabled" ng-model="locations.isChecked" ng-true-value="CHECKED" ng-false-value="UNCHECKED" ng-change="filterGrid(location)">
    <label>{{ location }}</label>
</span>

data i in list format

0: "One"
1: "Two"
2: "Three"
3: "USA"
4: "India"
5: "Japan"
6: "China"



 $scope.model.locations = data.location;
    for(var i=0;i<$scope.model.locations.length;i++){
         if($scope.model.locations[i] == 'USA'){
        $scope.model.locations[i].isChecked ="CHECKED"
        }
    }

But I am not able to make USA checked default, may be because of this is in string format.

Please suggest

Upvotes: 0

Views: 79

Answers (1)

rllola
rllola

Reputation: 431

I believe you can use ng-checked : Angularjs doc on ng-checked

I try it and I have it by default, here my code :

main.js

  $scope.locations = [
{ name : "One"},
{ name : "Two"},
{ name : "Three"},
{ name : "USA"},
{ name : "India"},
{ name : "Japan"},
{ name : "China"} ];

main.html

<span ng-repeat="location in locations">
  <input type="checkbox" ng-checked="location.name == 'USA'">
  <label>{{ location.name }}</label>
</span>

I hope it will help.

Upvotes: 1

Related Questions