0

I am building an app where I will be having radio buttons something like gender.

all the data is coming from the server and even which radio button need to be checked also.

I am keeping that data in ng-checked but it is not updating the ng-model.

by using that model I am working on some other divs to be shown because ng-model is not updating I could not show that data.

Can anybody help me to solve this issue? or any suggestions that custom directives can update the ng-model by taking the data from the ng-change or ng-checked

even I am setting ng-model dynamically, like this everything in ng-repeat 

<input type="{{controls.type}}" class="form-control input-lg mandatory mapping_input radio" id="{{$parent.$parent.$index}}_{{controls.id}}" a ng-model="formData[$parent.$parent.$index][controls.name]" value="{{controls.id}}" name="control_{{$parent.$parent.$index}}_{{controls.name}}" ng-class="{checkbox_item : element.type == 'checkbox'}" autocomplete="off" ng-checked="setthecheckboxvalue(formData[$parent.$parent.$index][controls.name],controls.value)" ng-required="controls.mandatory">
  • Could you explain why dafault behavior for radio buttons and ng-model don't solve your problem? https://docs.angularjs.org/api/ng/input/input%5Bradio%5D seems to be what you need as far as I can tell – Rotem B Dec 01 '16 at 14:27
  • https://docs.angularjs.org/api/ng/directive/ngChecked says `Note that this directive should not be used together with ngModel, as this can lead to unexpected behavior`. Hope this clears your doubt. The answer posted below is the right one. – EmptyCup Dec 01 '16 at 14:53
  • Possible duplicate of [AngularJS - Binding radio buttons to models with boolean values](http://stackoverflow.com/questions/16970248/angularjs-binding-radio-buttons-to-models-with-boolean-values) – EmptyCup Dec 01 '16 at 14:55
  • @RotemB - default behavior ng-model is not working because. even ng-model is generated dynamically. So I could not assign any value to the ng-model, Is there any possibility I check the radio box in the view itself or by passing some parameters to function in the controller – raghuveer ambedkar Dec 01 '16 at 17:43
  • Can you add more code? – Isma90 Dec 06 '16 at 16:24

1 Answers1

2

You don't need ng-checked for this purpose. Just use ng-model and value attribute of the input element

i.e.

<input type="radio" name="male" value="male" ng-model="data.gender"> Male
<input type="radio" name="female" value="female" ng-model="data.gender"> Female

It will be checked as data received from server is either male or female

Hope this helps!

Rajesh Dan
  • 459
  • 7
  • 16
  • I have everything dynamic even ng-model will this code works with this cae. I have edited my question with code. Can you please check – raghuveer ambedkar Dec 01 '16 at 17:26
  • @raghuveerambedkar Note that according to angular's documentation - ng-checked should not be put together with ng-model and this could lead to unexpected behavior. https://docs.angularjs.org/api/ng/directive/ngChecked – Rotem B Dec 04 '16 at 06:02