5

EDIT: My code actually does work, I was just being an idiot with an unrelated problem. Thanks for your input everyone.

So I have an array of JSON objects formatted like this:

[{"id":"id1", "text":"text1"}, {"id":"id2", "text":"text2"},....]

I want to populate an AngularJS select field using these, with the text fields as the display text and the id fields as the values, or whatever binds to the model. I have looked around, but can't for the life of me figure out what I need to do.

Right now I have this for my select, which results in nothing displaying:

<select name="field" ng-model="npe.formData.field" ng-options="field.id as field.text for field in fields">

Doing ng-options this way results in things displaying, but obviously will result in the incorrect value binding to the model:

ng-options="field as field.text for field in fields"

I have seen people talking about using "(key, value)", but I can't wrap my head around how it works.

4
  • What do you want as result and can you post an example of what's wrong? Both your ng-options example directive usages are fine. Commented Jun 13, 2014 at 19:15
  • Neither work as I would like them to. My first example results in nothing being displayed in the menu. My second results in proper displays, but the value does not bind to the model correctly (it sets the entire JSON object to the value, rather than just the id). I hoped I had made that clear in the post. Any recommendations on making it more clear? Commented Jun 13, 2014 at 19:16
  • Where's the dutyOrg coming from? Is it a part of field? Commented Jun 13, 2014 at 19:28
  • Failed typo making edits, fixed. Commented Jun 13, 2014 at 19:29

2 Answers 2

10

You'll need (key,value) in case you are repeating through a map, which is not the case - in your example you are iterating an array of objects. An usage of (key,value) would be:

$scope.map={item1:'a',item2:'b'};
...
<li ng-repeat="(id,text) in map">{{id}}) {{text}}</li>

which would render "item1) a" and so on.

Why did you mention the snippet below doesn't work?

ng-options="field.id as field.text for field in fields"

Checkout this fiddle - http://jsfiddle.net/7eqsc/2/, it works like a charm and updates the model with the correct id.

<div ng-app="myApp">
<div ng-controller="myCtrl">

    <div ng-repeat="(id,text) in notes">{{text}}</div>
    <select ng-model="model.id" ng-options="note.id as note.text for note in notes" ></select>

    Selected id:{{model.id}}

</div>

Sign up to request clarification or add additional context in comments.

2 Comments

can u explain what is the meaning of note.id as note.text for note here.. I am new to this
sure, in this example I assume you have an array of "notes" that are basically simple objects that contain text and id properties, for example: var note = {text: 'My note', id: 'some-id'}; The "note.id as note.text" syntax is called a "comprehension expression" and can be really confusing. What I basically did is assigned the value of the option to note.id and the label to note.text. You can learn more about it in the ngOptions docs.
3

Here's a sample Fiddle, which works with a basic controller assigning some values. Although you don't have a full code sample I expect not using ng-model is the issue you're facing.

function TodoCtrl($scope) {
    $scope.fields = [{"id":"id1", "text":"text1"}, {"id":"id2", "text":"text2"}];
    $scope.choice = null;
}

And the HTML

<div ng-app>
  <div ng-controller="TodoCtrl">
      <h2>Chosen = {{ choice }}</h2>
      <select ng-options="field.id as field.text for field in fields" ng-model="choice"></select>
  </div>
</div>

What you should probably take from this is that, ng-options requires ng-model to work, otherwise it will just do nothing.

2 Comments

I do have ng-model, I just did not post it. Edited OP accordingly, as my full code is more or less identical to yours.
I searched SO and the AngularJS docs for hours just trying to find a simple example like yours doing an ng-repeat for a select box. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.