0

I have two problems (maybe because of the same bug):

1.) I can't default check one radio button in my html page. All three radioboxes are unchecked.

2.) In my code the value for selectedValue is always undefined. The other value username works in my code. (But the value of selectedValue works in the "tt" section and will be changed when i click the radio buttons)

HTML Code:

<input class="login__input" type="text" name="username" ng-model="username" placeholder="Username" autocorrect="off" autocapitalize="off">

 <div class="radio__box">
      <label>
    <input type="radio" ng-model="selectedValue" value="value1" checked>
    Radio1
  </label><br/>
  <label>
    <input type="radio" ng-model="selectedValue" value="value2">
    Radio2
  </label><br/>
  <label>
    <input type="radio" ng-model="selectedValue" value="value3">
    Radio3
  </label><br/>

  <tt>selected = {{selectedValue}}</tt><br/>

  </div>

Javascript Code:

var username = $rootScope.username; // works
var selectedValue = $rootScope.selectedValue; // undefined

Any help would be great. Thanks in advance!

3
  • Try putting the label tag before the radio button instead of nesting the radio button inside the label tag Commented Aug 18, 2015 at 13:35
  • 1
    Any chance we could see your controller code? Where do you initialize $scope.selectedValue? Commented Aug 18, 2015 at 13:36
  • That's the solution :D. I somehow missed it in the controller. Thanks man! Commented Aug 18, 2015 at 13:40

2 Answers 2

2

Here's a very simple plunk that demonstrates a working radio button with a default choice.

http://plnkr.co/edit/nTAcLlYIQk2Uk6887hPv?p=preview

JS:

angular.module('myApp', [])
.controller('myCtrl', function($scope) {
  $scope.selectedValue = "value1"
})
Sign up to request clarification or add additional context in comments.

Comments

0

Like Fissio pointed out, i missed to initialize the variable.

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.