pkdkk
pkdkk

Reputation: 3961

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?

Upvotes: 1

Views: 2918

Answers (2)

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11398

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

Upvotes: 2

Romain F.
Romain F.

Reputation: 794

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

Upvotes: 0

Related Questions