krishna_v
krishna_v

Reputation: 1511

set default value of false to radio button in angularjs

I am trying to set the value of radio button to false. Only when clicked it should be set to true. This is my code:

<input type="radio" name="data" ng-value="true" ng-model="ERRORS">ERRORS<br>
<input type="radio" name="data" ng-value="true" ng-model="OCCURRENCES">OCCURRENCES<br>
<input type="radio" name="data" ng-value="true" ng-model="STATUS">STATUS<br>

 <div>
   Selected Data Value:
   Selected Data<br>
    ERRORS: {{ ERRORS }}<br>
    OCCURRENCES: {{OCCURRENCES }}<br>
    STATUS: {{ STATUS }}<br>
  </div>   

However i am getting the value as "true" if radio button selected and empty when it is not selected.

Upvotes: 0

Views: 2033

Answers (2)

yeouuu
yeouuu

Reputation: 1913

You can do this with ng-init or in your controller(I recommend this way):

Examples:

<input type="radio" name="data" ng-init="ERRORS= false" 
    ng-value="true" ng-model="ERRORS">ERRORS<br>
<input type="radio" name="data" ng-init="OCCURRENCES= false" 
    ng-value="true" ng-model="OCCURRENCES">OCCURRENCES<br>
<input type="radio" name="data" ng-init="STATUS= false" 
    ng-value="true" ng-model="STATUS">STATUS<br>

Or you could initialize the values inside your controller:

$scope.ERRORS = false;
...

Upvotes: 1

AranS
AranS

Reputation: 1891

You could use an old javascript trick - an empty string is perceived is a false boolean.

Upvotes: 1

Related Questions