Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:11:47+00:00 2026-06-16T05:11:47+00:00

I’m trying to render select box and it does not work as expected –

  • 0

I’m trying to render select box and it does not work as expected – options values are incorrect. I checked manual, according to it the syntax for array (in my case array of objects) is

select as label for value in array

So here is what i’m doing:

data:

[{"id":"3","name":"asdasd","code":"asdads","group":"2","cost":"0"},{"id":"4","name":"adrf fg df ","code":"dasfasd","group":"2","cost":"0"}]

template:

<select ng-model="productToBuy" ng-options="item.id as item.id for item in products"></select>

rendered result:

<select ng-model="productToBuy" ng-options="item.id as item.id for item in products" class="ng-pristine ng-valid">
    <option value="0" selected="selected">3</option>
    <option value="1">4</option>
</select>

as we can see, options values does not set to items’s id.

Also this may be not proper syntax when source is array but i having same results when trying like this:

<select ng-model="productToBuy" ng-options="item.id as item.id for (key, item) in products"></select>

I put this code on jsfiddle. Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:11:48+00:00Added an answer on June 16, 2026 at 5:11 am

    This is the behavior of the ngOptions directive (the output you are seeing, where the value is the index of the item and not the id property that you are trying to pass in from your code sample). ngOptions will data-bind the selected option to the variable that you specified in ngModel. You would then work with your data bound variable instead of the “value” of the option element itself.

    Html:

    <select ng-model="selected" ng-options="item.name for item in items"></select> {{ selected }}
    

    JS:

    $scope.items = [
        {"id": "3","name":"asdasd","code":"asdads","group":"2","cost":"0"},
        {"id": "4","name":"adrf fg df ","code":"dasfasd","group":"2","cost":"0"}
    ];
    

    In the above example, $scope.selected would represent the actual selected item. You can then access any of the selected item’s properties: $scope.selected.name, $scope.selected.code… etc.

    If you needed to preselect an item with the above example, you could do the following:

    $scope.items = [
        {"id": "3","name":"asdasd","code":"asdads","group":"2","cost":"0"},
        {"id": "4","name":"adrf fg df ","code":"dasfasd","group":"2","cost":"0"}
    ];
    $scope.selected = $scope.items[1]; // Pre-selected the 2nd item in your array
    

    If you still need to have full control over your value attributes, you’re better off using the ng-repeat directive, but remember if you do this, your selected items won’t be data bound to your model.

    EDIT: Note on ‘select as label for value in array’ syntax:

    In case its helpful, what your “item.id as item.name for item in products” was doing was actually setting your variable in the ngModel directive to the value you specify in the select portion of the syntax. So what that expression is doing is setting the label as item.name, but binding $scope.selected to the value of item.id, rather than to the entire instance of item itself. So if your first item in the examples above was selected, $scope.selected would be equal to “3”. It doesn’t actually change the value attribute of the option element itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.