Search code examples
angularjsangular-ngmodel

AngularJS ng-model update value on condition


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?


Solution

  • 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