1

When my model value is true then I want the radio buttons to be selected when loaded, but its happening the otherway around. All false models are getting selected. How do fix this.

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

angular.module('radioExample', [])
      .controller('ExampleController', ['$scope',
        function($scope) {
          $scope.kind = [{
            name: 'task',
            selected: false
          }, {
            name: 'bug',
            selected: false
          }, {
            name: 'other',
            selected: true
          }, {
            name: 'rfe',
            selected: false
          }]
          $scope.$watch('kind', function() {
            console.log('changed', JSON.stringify($scope.kind, null, 2))
          }, true)

        }
      ]);
1

5 Answers 5

10

use ng-value here is the doc for angular radio

<input type="radio" name="" id="" value="" ng-model="k.selected" ng-value="true" />

then, if the ng-value is true and the model value is also true then check box will checked

here is the update Demo

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

3 Comments

yes this works! Is there a setting to toggle the selection? Because right now it it selects more than one radio button at once, I need to be able to select only one option a time
glad to help you , :) and if you want to select only one radio then you have to use same model for each radio buttons, in your case there are different models for each and every radio buttons, but you can do something like this plnkr.co/edit/pOzHeDvqz32qIxBGexvL?p=preview :)
Here, you deserve a cookie!
4
<input type="checkbox" id="rempass" ng-model="rememberMe" ng-checked="rememberMeUserInfoCheck()" >  $scope.rememberMeUserInfoCheck=function() { return true; } 

this works for me

3 Comments

can you please elaborate?
<input type="checkbox" id="rempass" ng-model="rememberMe" ng-checked="rememberMeUserInfoCheck()" > $scope.rememberMeUserInfoCheck=function() { return true; } this works for me
sorry guys my mistake
3

Add ng-checked ="true" to your radio input field

`<input type="radio" ng-model="modelName" name="radioName" value="value1" ng-checked="true">`

1 Comment

This is the only thing that works for me. Not ng-model nor ng-value.
1

I fixed the plunker plunker

 <form name="myForm" ng-controller="ExampleController">
    <br />all 'false' radio buttons are selected when 'value' is used -------------
    <br />
    <div ng-repeat="k in kind">
      <input type="radio" name="" id="" value="" ng-model="!k.selected" value="k.selected" />{{k.name}}
    </div>
    <br />all radio buttons are selected when 'ng-value' is used -------------
    <br />
    <div ng-repeat="k in kind">
      <input type="radio" name="" id="" value="" ng-model="k.selected" ng-value="k.selected" />{{k.name}}
    </div>
</form>

you had it right.... just needed to add a ! so the model will take the opposite of the scope value... since you are using them for both I guess its wont hurt your code

1 Comment

Thanks for the response Jony
0

For those working with FormGroup and FormControl:

In the template:

  1. Add formControlName = sameNameforAllRadioButtonsOfAChoice as an attribute to your radio button input tags.
  2. Also add value = "true" and value = "false" (you can also do it with numbers and strings, but I will continue with boolean).

In the component:

  1. Add the name, e.g. sameNameforAllRadioButtonsOfAChoice, to your FormGroup:

myForm = new FormGroup({sameNameforAllRadioButtonsOfAChoice: new FormControl('false')})

This sets the default value to false. In the FormControl, be careful to write it as a string!

BONUS - If you need Validation:

  1. import { FormControl, FormGroup, Validators } from '@angular/forms';
  2. myForm = new FormGroup({sameNameforAllRadioButtonsOfAChoice: new FormControl('false', [Validators.required])})

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.