-1

I have a scoped object in my controller which sets some default values in my app, like this:

  //set default values for form
  $scope.appText = {
    'size':'normal',
    'colour':'green',
    'alignment':'top',
    'dimensions':{
      'width':600,
      'height':300
    }
  };

In my view I have a series of HTML radio buttons, each with an ng-value which has an object with a width and a height, like so:

<input type="radio" name="radio-dimensions" ng-value="{width:600, height:300}" ng-model="appText.dimensions" checked>600x300

<input type="radio" name="radio-dimensions" ng-value="{width:300, height:250}" ng-model="appText.dimensions">300x250

I can't make the first radio button be checked by default, even though its ng-value matches the model.

How do I make it checked by default? I've tried adding

ng-checked="true"

which doesn't work, and I don't think is recommended. This jsfiddle illustrates the problem:

https://jsfiddle.net/yod71dm8/1/

3
  • In this jsfiddle ng-checked works. Maybe this can help you: stackoverflow.com/questions/14530785/… Commented Mar 13, 2016 at 23:04
  • jsfiddle.net/yod71dm8/7 check this out Commented Mar 13, 2016 at 23:37
  • checked doesn't work in angular, you need to mention $parent.somevariable in ng-model to get radio button selected Commented Mar 13, 2016 at 23:53

2 Answers 2

1

your ng-model needs to reference something that is a boolean value ie. either true or false for a radio button or checkbox.

so in your controller you would have $scope.radio1 = true

then in your view

<input type="radio" name="radio-dimensions" ng-model="radio1">600x300
Sign up to request clarification or add additional context in comments.

Comments

0

ng-checked="true" does work. My issue was that the radio buttons were inside an ng-if statement. Changing ng-if to ng-show fixed the issue.

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.