3

I have {{policy.isActiveInPolicyGroup}} which is Boolean, I want the radio input be checked if it was true.

<div ng-repeat="policy in item.policies">
    <input type="radio" ng-model="item.selectedPolicy" id="policy_{{$index}}"
           name="test" ng-value="policy.id">
    <label class="custom-control-label" for="test">
         {{item.name}}
    </label>
</div>
georgeawg
  • 48,608
  • 13
  • 72
  • 95

3 Answers3

0

Only set ng-model value like following:-

$scope.item.selectedPolicy = $scope.policy.id;

It will be checked if its ng-true value matched with its ng-model value.

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
0

The first thing you should know it's about value of inputs with radio type:

Recommended

the radio inputs can't define by Boolean, if inputs are more than 2 we can't define value as Boolean so we should set a different value for each of them.

Ex: <input type="radio" name="test" ng-value="1">, <input type="radio" name="test" ng-value="2"> , <input type="radio" name="test" ng-value="3">

Not Recommended

Note: Except when input number is 2: in this case we can set value false or true

Ex: <input type="radio" name="test" ng-value="true">, <input type="radio" name="test" ng-value="false">

radio input Note:

all radio inputs in one group should have same name


checkbox input:

inputs with checkbox type by default define with Boolean value because we can set different name for each of them, and result is checked [true] or not [false]


ng-model and ng-value

Radio input should have two attributes ng-model and ng-value that because in angularjs logic ng-model should equal ng-value and can't do it by one attribute.

Community
  • 1
  • 1
Maher
  • 2,517
  • 1
  • 19
  • 32
0

DEMO of Radio Buttons that Use Boolean Values1

  <script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app>
    <fieldset>
      <input type="radio" name="radio"
             ng-model="model" ng-value="false">false<br>
      <input type="radio" name="radio" 
             ng-model="model" ng-value="true">true<br>
    </fieldset>
    <fieldset>
      <input type="checkbox" ng-model="model">check<br>
    </fieldset>
    model={{model}}   
  </body>

For more information, see

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95