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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

.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; } } }); } }; }])
0

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

2 Comments

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.
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: stackoverflow.com/questions/19281404/…

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.