Iman Sedighi Iman Sedighi - 6 months ago 20
AngularJS Question

Check a radio button if ngModel is not equal to a fixed value

I have two radio buttons. I wanna check the first radio button if ngModel has value of X, and if ngModel has any value other than X then the second radio button must be checked. It means the second radio button doesn't have a fixed value. It can be anything except X.
I can't achieve this with following code:

<input type="radio" ng-model="color.name" value="x">
<input type="radio" ng-model="color.name" value="">


So in this case if color.name is equal to x then first radio is checked but if color.name is not equal to x then none of the radio are checked by default.
How can I fix the second radio button's problem?

Answer
<input type="radio" ng-model="color.name" ng-checked="color.name == 'x'" value="x">