Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing to note is that ngModel is <em>required</em> for ngOptions to work... note the <code>ng-model="blah"</code> which is saying "set $scope.blah to the selected value".</p> <p>Try this:</p> <pre><code>&lt;select ng-model="blah" ng-options="item.ID as item.Title for item in items"&gt;&lt;/select&gt; </code></pre> <p>Here's more from AngularJS's documentation (if you haven't seen it):</p> <blockquote> <p>for array data sources:</p> <ul> <li>label for value in array</li> <li>select as label for value in array</li> <li>label group by group for value in array = select as label group by group for value in array</li> </ul> <p>for object data sources:</p> <ul> <li>label for (key , value) in object</li> <li>select as label for (key , value) in object</li> <li>label group by group for (key, value) in object</li> <li>select as label group by group for (key, value) in object</li> </ul> </blockquote> <hr> <p>For some clarification on option tag values in AngularJS:</p> <p>When you use <code>ng-options</code>, <strong>the values of option tags written out by ng-options will always be the index of the array item the option tag relates to</strong>. This is because AngularJS actually allows you to select entire objects with select controls, and not just primitive types. For example:</p> <pre class="lang-js prettyprint-override"><code>app.controller('MainCtrl', function($scope) { $scope.items = [ { id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'blah' } ]; }); </code></pre> <pre class="lang-html prettyprint-override"><code>&lt;div ng-controller="MainCtrl"&gt; &lt;select ng-model="selectedItem" ng-options="item as item.name for item in items"&gt;&lt;/select&gt; &lt;pre&gt;{{selectedItem | json}}&lt;/pre&gt; &lt;/div&gt; </code></pre> <p>The above will allow you to select an entire object into <code>$scope.selectedItem</code> directly. <strong>The point is, with AngularJS, you don't need to worry about what's in your option tag. Let AngularJS handle that; you should only care about what's in your model in your scope.</strong></p> <p><a href="http://plnkr.co/edit/SxIvt4KThWLtWvh3PnOh?p=preview" rel="noreferrer">Here is a plunker demonstrating the behavior above, and showing the HTML written out</a></p> <hr> <p>Dealing with the default option:</p> <p>There are a few things I've failed to mention above relating to the default option.</p> <p><strong>Selecting the first option and removing the empty option:</strong></p> <p>You can do this by adding a simple <code>ng-init</code> that sets the model (from <code>ng-model</code>) to the first element in the items your repeating in <code>ng-options</code>:</p> <pre><code>&lt;select ng-init="foo = foo || items[0]" ng-model="foo" ng-options="item as item.name for item in items"&gt;&lt;/select&gt; </code></pre> <p>Note: This could get a little crazy if <code>foo</code> happens to be initialized properly to something "falsy". In that case, you'll want to handle the initialization of <code>foo</code> in your controller, most likely.</p> <p><strong>Customizing the default option:</strong></p> <p>This is a little different; here all you need to do is add an option tag as a child of your select, with an empty value attribute, then customize its inner text:</p> <pre><code>&lt;select ng-model="foo" ng-options="item as item.name for item in items"&gt; &lt;option value=""&gt;Nothing selected&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Note: In this case the "empty" option will stay there even after you select a different option. This isn't the case for the default behavior of selects under AngularJS.</p> <p><strong>A customized default option that hides after a selection is made:</strong></p> <p>If you wanted your customized default option to go away after you select a value, you can add an ng-hide attribute to your default option:</p> <pre><code>&lt;select ng-model="foo" ng-options="item as item.name for item in items"&gt; &lt;option value="" ng-if="foo"&gt;Select something to remove me.&lt;/option&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload