59

I need to make a system to change the total price by number of tickets selected. I created some radio buttons to choose the number of ticket. The ploblem is that the default value is unset and the result is null on loading.

<input type="radio" ng-model="people" value="1" checked="checked"><label>1</label>
<input type="radio" ng-model="people" value="2"><label>2</label>
<input type="radio" ng-model="people" value="3"><label>3</label>
<ul>
  <li>{{10*people}}€</li>
  <li>{{8*people}}€</li>
  <li>{{30*people}}€</li>
</ul>

Please see my jsfiddle.

2
  • what default value you want to set Commented Apr 5, 2013 at 12:21
  • 1
    side note for anyone getting here from using {{$index}} for value, your model needs to stringify the initial value '1'. Commented Dec 12, 2016 at 3:28

5 Answers 5

118

Set a default value for people with ngInit

<div ng-app>
    <div ng-init="people=1" />
        <input type="radio" ng-model="people" value="1"><label>1</label>
        <input type="radio" ng-model="people" value="2"><label>2</label>
        <input type="radio" ng-model="people" value="3"><label>3</label>
    <ul>
        <li>{{10*people}}€</li>
        <li>{{8*people}}€</li>
        <li>{{30*people}}€</li>
    </ul>
</div>

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

6 Comments

you can also do so inside the controller by setting scope.people=1, which is the recommended way described in docs.angularjs.org/api/ng.directive:ngInit
@Arun, What if values have characters instead of digit. Then how would be the code for ng-init look like?
@MrSuS people='chars'
using ng-init is bad practice. this approach is only effecitive for a Form being populated for the 1st time. If you have values and assigned them to model binders, ng-init will overwrite your values.
what if I used ng-value and for default radio I want to use ng-value = "" because that model I use to filter on a list later on, and the default filter shouldn't filter on anything, hence empty string?
|
25
<div ng-app="" ng-controller="myCntrl">    
        <input type="radio" ng-model="people" value="1"/><label>1</label>
        <input type="radio" ng-model="people" value="2"/><label>2</label>
        <input type="radio" ng-model="people" value="3"/><label>3</label>
</div>
<script>
    function myCntrl($scope){
        $scope.people=1;
    }
</script>

Comments

15

why not just ng-checked="true"

2 Comments

Because it would check the radio button visually, but would not set the value of "people" like if u had click on the radio button through a browser.
The answers section is not a good place for questions. This would be better in comments on the accepted answer.
1

In Angular 2 this is how we can set the default value for radio button:

HTML:

<label class="form-check-label">
          <input type="radio" class="form-check-input" name="gender" 
          [(ngModel)]="gender" id="optionsRadios1" value="male">
          Male
</label>

In the Component Class set the value of 'gender' variable equal to the value of radio button:

gender = 'male';

Comments

-6
<input type="radio" name="gender" value="male"<%=rs.getString(6).equals("male") ? "checked='checked'": "" %>: "checked='checked'" %> >Male
               <%=rs.getString(6).equals("male") ? "checked='checked'": "" %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.