chandru
chandru

Reputation: 157

ng--repeat Radio button default check

I am new to the angularjs. I have a set of radio buttons and respective values I am displaying using ng-repeat and I want the first radio button to be checked by default.

<div class="form-group form-radio" ng-repeat="insr in oneClickDtls.response.investAndInsrStrategy.insuranceOptions">
  <div class="col-sm-8 col-md-8 visible-sm visible-md">
     <div class="radio padding-top12 padding-bottom12">
        <input class="form-input" type="radio" id="rb_{{$index}}" name="insuranceOptionTab" value="{{insr.coverId}}" ng-model="oneClick.submitOneClickDetails.insuranceOptionsSubmit.coverID">
        <label class="form-label bold-one-click" for="rb_{{$index}}">{{insr.coverAmt | currency}} | {{insr.premium | currency}}</label>
     </div>
  </div>
</div>

any help would be appreciated. thank you.

Upvotes: 0

Views: 1766

Answers (2)

K K
K K

Reputation: 18099

You can use $index to select first input

Like:

ng-checked="$index==0?true:false"


<input ng-checked="$index==0?true:false">

If you also want to set the ng-model value, then you will need to initialize ng-model value to the first item in the iteration and then checking the input based on the ng-model value.

example:

<input ng-checked="yourModel==iterationItem" />

where yourModel is the ng-model and iterationItem will be the iteration item.

Upvotes: 2

daCoda
daCoda

Reputation: 3845

Try this:

  • In your javascript, set the default value for the ng-model (oneClick.submitOneClickDetails.insuranceOptionsSubmit.coverID), as the first value of your radio buttons.

Upvotes: 0

Related Questions