Skip to content

Instantly share code, notes, and snippets.

@codeshrew
Created October 25, 2014 17:04
Show Gist options
  • Save codeshrew/2edbb4163fac7f9aba03 to your computer and use it in GitHub Desktop.
Save codeshrew/2edbb4163fac7f9aba03 to your computer and use it in GitHub Desktop.
Angular select directive with initial selection and track by
<!-- This is an example for initiating the selection in a dropdown to a saved setting
In this case, we're using mongo to store a collection of programs and another to store a collection of projects
The projects reference the ObjectId of a program at `project.program` -->
<select
ng-init="selectedProgram.program = { _id: project.program }"
ng-change="project.program = selectedProgram.program._id"
ng-model="selectedProgram.program"
ng-options="program.name for program in programs track by program._id">
<!-- To have a default selection if there is no match to a program _id, just set an option with an empty value -->
<option value="">&ndash; Select a program &ndash;</option>
</select>
@codeshrew
Copy link
Author

I made a handy little AngularJS directive for handling dropdowns in a sane way: https://github.com/codeshrew/angular-sane-dropdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment