1

I'm pretty new to AngularJS, and have a (maybe simple or stupid) question.

I have a input radio box, with a ng-model, the value depends on a condition

<input type="radio" name="q_{{question.id}}" ng-model="question.option.id" value="{{!survey.isOptionSelected(question.related_question_option_id) ? 0 : opt.id}}">

my problem is that ng-model="question.option.id" is not changing when (or if) the condition changes .. is there a way update it with the current value?

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
pkdkk
  • 3,905
  • 8
  • 44
  • 69

2 Answers2

2

Maybe I'm wrong, but looking at your code, it looks like you're trying to enable/disable some radio buttons depending on a checkbox.

<input type="radio" name="q_{{question.id}}" 
       ng-model="question.option.id" 
       value="{{opt.id}}" 
       ng-disabled="survey.isOptionSelected(question.related_question_option_id)" />

Ain't this a better option?

And, if your condition changes, you must add a $watch on the select that can change, and update the question.option.id inside the callback

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
  • .directive('surveyOption', [function () { return { restrict: 'A', scope : { question: "=surveyOption" }, link: function (scope, iElement, iAttrs) { iAttrs.$observe('disabled', function(disabled) { if(disabled) { if(typeof scope.question.option !== "undefined") { scope.question.option.id = 0; } } }); } }; }]) – pkdkk Aug 07 '15 at 08:29
0

You need to replace value with ng-value: https://docs.angularjs.org/api/ng/directive/ngValue

Romain F.
  • 766
  • 10
  • 15
  • it's a start ;) ... but it still not update the question.option.id when the condition changes, I can see the value updates to "0" but not the question.option.id. – pkdkk Aug 07 '15 at 08:02
  • Right. Maybe you should try to bind on "option" instead of option.id. This way, if an option content change, the object reference will still be the same, and you won't need to do anything else. The drawback is that you must refer to the same object. Maybe this can help: http://stackoverflow.com/questions/19281404/object-equality-comparison-for-inputradio-with-ng-model-and-ng-value – Romain F. Aug 07 '15 at 08:26