0

I have a select element with ng-options to populate options and <option value="">Select</option> as a static default option. I want to show another item in options list - Other, if selected, renders a textbox for user to enter other value. I tried adding another static option with value="-1" but it didn't work.

3
  • Why don't you add that option to ng-options too? That way, you could watch the model on the select and when it changes to "Other", you then show the textbox using ng-show? Commented Jun 20, 2013 at 4:46
  • @callmekatootie Yes I thought about it. I'm wondering if there are better way of doing it. Commented Jun 20, 2013 at 5:22
  • Could you provide a plunkr of your code so that we can analyze it further? Commented Jun 20, 2013 at 5:39

1 Answer 1

2

I would try following:

<div ng-init="arr = [{name:'A', val:1},{name:'B', val:2},{name:'C', val:2}]">
  <select ng-model="v">
    <option ng-repeat="o in arr" value="o.val">{{ o.name }}</option>
    <option>Other</option>
  </select>
  <input type="number" ng-model="otherval" ng-show="v == 'other'"/>
</div>

or,

<div ng-init="arr = [{name:'A', val:1},{name:'B', val:2},{name:'C', val:2}]">
  <select ng-model="v" ng-options="o.val as o.name for o in arr.concat([{name:'Other',val:'other'}])">
  </select>
  <input type="number" ng-model="otherval" ng-show="v == 'other'"/>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I went ahead with adding Other option in my options collection. Only downside is that I'll have to validate that if selected value is other then discard select model value and take the other input value

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.