2

I have an array

terms = [5,10,15,20,25]

and I'm trying to use ng-options in a select statement

<select ng-model="myNumbers" ng-options="term + ' years' as term for term in terms">

which produces

<option value="?"></option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>    

What I want it to produce is

<option value="5">5 years</option>
<option value="10">10 years</option>
<option value="15">15 years</option>
<option value="20">20 years</option>
<option value="25">25 years</option>

Basically trying to solve 3 problems.

  1. Set the label correctly (5 years, etc)
  2. Set the value correctly (value="5", etc)
  3. Remove the initial blank option (start at 5)
5

Simple adjustment to move the string to the as portion

<select  ng-options="term  as term + ' years' for term in terms"></select>

DEMO

|improve this answer|||||
  • nice. now how to set the value correctly and remove the initial blank option? – sfletche Aug 13 '15 at 22:33
  • you already have the value in the ng-model variable. Refresh my demo and select something – charlietfl Aug 13 '15 at 22:35
  • if you set one option inside <select> can make it display what you want – charlietfl Aug 13 '15 at 22:38
  • use ng-init for initialising the ng-model. Then the initial blank entry will be gone. <select ng-init="myNumbers=terms[0]" ng-options="term as term + ' years' for term in terms"></select> – MjZac Aug 13 '15 at 22:43
  • 1
    yes, I know. I suggested it for an easy fix. It should be done from controller. – MjZac Aug 13 '15 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.