0

I'm trying to set a default selection for a radio button, but it's not working when used along side AngularJS directive ngModel

<input type="radio" value="5000" checked ng-model="status">5000

Here is the DEMO

1
  • Can you explain why is any different the answer you marked as valid and mine that you downvoted? Commented Jan 29, 2015 at 14:06

2 Answers 2

1

You can use ng-checked in this case. Updated Fiddle

HTML Code:

<div ng-app="myApp">
    <div ng-controller="test">
        <input type="radio" value="5000" ng-checked="status">5000
        <br/> <span>{{status}}</span>

    </div>
</div>

JS Code:

angular.module("myApp", [])

    .controller("test", function ($scope) {
    $scope.status = true;
});

UPDATE as per the OP comment:

fiddle

JS Code:

angular.module("myApp", [])

    .controller("test", function ($scope) {
    $scope.status = true;
    if (!$scope.status) $scope.radioValue = 0;
    else $scope.radioValue = 5000;
});

HTML Code:

<div ng-app="myApp">
    <div ng-controller="test">
        <input type="radio" value="5000" ng-checked="status" ng-model="radioValue">5000
        <br/> <span>{{radioValue}}</span>

    </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

but I also want the value of radio button selected .....for which I would require ngModel .... What do u think ??
Just define a model as well and initialize that in the controller as well.
@iJay: Updated the code, you can put the false value and see that the value is getting changed.
0

You need to initialize the status scope property in your controller:

HTML:

<input type="radio" value="5000" checked ng-model="status">5000

JS:

angular.module("myApp", []).controller("test", function ($scope) {
    $scope.status = 5000;
});

DEMO

1 Comment

What do you mean it doesn't set a default value, explain what do you expect to happen then

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.