0

I have a select box with options being generated like this:

<select ng-model="module.sectionID">
  <option ng-repeat="section in sections" value="{{section.$id}}">
     {{section.title}}
  </option>
</select>

As you can see the value of the option is arbitrary, so I can't set the default value like this:

ng-init="module.sectionID='this value will change'"

I want the default value to be the first option in the select box, right now it is always a null / blank value, until they make a selection, which isn't very user friendly.

2
  • 1
    it would be module.sectionID = sessions[0].$id Commented Nov 29, 2015 at 18:55
  • Do I put that on the ng-model or ng-init? Commented Nov 29, 2015 at 20:24

1 Answer 1

1
   <select ng-model="module.sectionID" ng-init="module.sectionID=sections[0].$id" ng-options="section.$id as section.title for section in sections">
   </select>
Sign up to request clarification or add additional context in comments.

6 Comments

That's an ok solution, but I don't want it to say -- choose section -- I want it to select the 1st valid option by default, so that there is always a valid choice made no matter what.
You can remove that 'option' tag and the 'init' would select the first option. I have edited my answer.
I tried that, and it reverts back to being blank by default
I'm doing it exactly the same way and it's still not working, I wonder if it's because I'm pulling data from firebase
Actually I see it flash the correct value for a little bit and then it reverts back to blank, the ng-init value get's wiped out.
|

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.