2

Is their a way to bind two ng-model in a select tag? To further explain this I will share some of my code

I have this dynamic input buttons, where if a user wishes to add a new option. Note I used two property opt.id & opt.text

<div ng-repeat="opt in q.options">
   <label><span>{{opt.id}}.</span></label>
   <input type="text" ng-model="opt.text" placeholder="Add new possible answer here.">
   <a ng-click="q.options.splice($index,1)"><i class="icon-remove-sign icon-large"></i> </a>
</div>

then I reused the q.options to display the ids in a select tag.

<div ng-repeat="answer in q.answer">
 <div>
    <select ng-model="answer.id" ng-options="c.id as c.id for c in q.options"></select>
    <a ng-click="q.answer.splice($index,1)"><i class="icon-remove-sign icon-large"></i></a>
 </div>
</div>

With this I was able to recreate the option ids and set the value ng-model="answer.id" to my controller but I also need the opt.text to set it to answer.text to be also fetch from my controller then pass it to my api call. I don't want to create another select tag for it.

added a jsfiddle: http://jsfiddle.net/si_dean_ako/3UFAf/1/

1 Answer 1

1
<select ng-model="select[$index]" ng-options="c.id for c in q.options" 
ng-change="change(answer,select[$index])"></select>

Controller:

$scope.select = {};

$scope.change = function(o,v){      
    o.id = v.id;
    o.text = v.text;
}
Sign up to request clarification or add additional context in comments.

6 Comments

tried what you suggested, it did saved but not what I expect. Now the response is like this: {id: { id: "a", text: "test"}, text: ""}. It should just have {id: "a", text: "test"}.
Change ng-model="answer.id" to ng-model="answer" and see if that is what you want.
tried, when saved gave me {id: "", text: ""} and also even though I select letter "a" when click it return to blank option.
Could you create a fiddle or update your question to share the contents of q?
sorry, first time setting up in jsfiddle but here: jsfiddle.net/si_dean_ako/3UFAf
|

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.