UNPKG

knowhow-server

Version:

A personal workflow engine that can manage and use any network connected host

145 lines (139 loc) 10 kB
<a href="http://github.com/angular/angular.js/tree/v1.2.9/src/ng/directive/select.js#L4" class="view-source btn btn-action"><i class="icon-zoom-in"> </i> View source</a><a href="http://github.com/angular/angular.js/edit/master/src/ng/directive/select.js" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable="">select</code> <div><span class="hint">directive in module <code ng:non-bindable="">ng</code> </span> </div> </h1> <div><h2 id="description">Description</h2> <div class="description"><div class="ng-directive-page ng-directive-select-page"><p>HTML <code>SELECT</code> element with angular data-binding.</p> <h3 id="description"><code>ngOptions</code></h3> <p>The <code>ngOptions</code> attribute can be used to dynamically generate a list of <code>&lt;option&gt;</code> elements for the <code>&lt;select&gt;</code> element using the array or object obtained by evaluating the <code>ngOptions</code> comprehension_expression.</p> <p>When an item in the <code>&lt;select&gt;</code> menu is selected, the array element or object property represented by the selected option will be bound to the model identified by the <code>ngModel</code> directive.</p> <p>Optionally, a single hard-coded <code>&lt;option&gt;</code> element, with the value set to an empty string, can be nested into the <code>&lt;select&gt;</code> element. This element will then represent the <code>null</code> or &quot;not selected&quot; option. See example below for demonstration.</p> <p>Note: <code>ngOptions</code> provides iterator facility for <code>&lt;option&gt;</code> element which should be used instead of <a href="api/ng.directive:ngRepeat"><code>ngRepeat</code></a> when you want the <code>select</code> model to be bound to a non-string value. This is because an option element can only be bound to string values at present.</p> </div></div> <h2 id="usage">Usage</h2> <div class="usage"><p>This directive can be used as custom element, but be aware of <a href="guide/ie">IE restrictions</a>.</p>as element:<pre class="prettyprint linenums">&lt;select ng-model="{string}" [name="{string}"] [required] [ng-required="{string}"] [ng-options="{comprehension_expression}"]&gt; &lt;/select&gt;</pre> <h4 id="usage_parameters">Parameters</h4><table class="variables-matrix table table-bordered table-striped"><thead><tr><th>Param</th><th>Type</th><th>Details</th></tr></thead><tbody><tr><td>ngModel</td><td><a href="" class="label type-hint type-hint-string">string</a></td><td><div class="ng-directive-page ng-directive-select-page"><p>Assignable angular expression to data-bind to.</p> </div></td></tr><tr><td>name <div><em>(optional)</em></div></td><td><a href="" class="label type-hint type-hint-string">string</a></td><td><div class="ng-directive-page ng-directive-select-page"><p>Property name of the form under which the control is published.</p> </div></td></tr><tr><td>required <div><em>(optional)</em></div></td><td><a href="" class="label type-hint type-hint-string">string</a></td><td><div class="ng-directive-page ng-directive-select-page"><p>The control is considered valid only if value is entered.</p> </div></td></tr><tr><td>ngRequired <div><em>(optional)</em></div></td><td><a href="" class="label type-hint type-hint-string">string</a></td><td><div class="ng-directive-page ng-directive-select-page"><p>Adds <code>required</code> attribute and <code>required</code> validation constraint to the element when the ngRequired expression evaluates to true. Use <code>ngRequired</code> instead of <code>required</code> when you want to data-bind to the <code>required</code> attribute.</p> </div></td></tr><tr><td>ngOptions <div><em>(optional)</em></div></td><td><a href="" class="label type-hint type-hint-comprehension_expression">comprehension_expression</a></td><td><div class="ng-directive-page ng-directive-select-page"><p>in one of the following forms:</p> <ul> <li>for array data sources:<ul> <li><code>label</code> <strong><code>for</code></strong> <code>value</code> <strong><code>in</code></strong> <code>array</code></li> <li><code>select</code> <strong><code>as</code></strong> <code>label</code> <strong><code>for</code></strong> <code>value</code> <strong><code>in</code></strong> <code>array</code></li> <li><code>label</code> <strong><code>group by</code></strong> <code>group</code> <strong><code>for</code></strong> <code>value</code> <strong><code>in</code></strong> <code>array</code></li> <li><code>select</code> <strong><code>as</code></strong> <code>label</code> <strong><code>group by</code></strong> <code>group</code> <strong><code>for</code></strong> <code>value</code> <strong><code>in</code></strong> <code>array</code> <strong><code>track by</code></strong> <code>trackexpr</code></li> </ul> </li> <li>for object data sources:<ul> <li><code>label</code> <strong><code>for (</code></strong><code>key</code> <strong><code>,</code></strong> <code>value</code><strong><code>) in</code></strong> <code>object</code></li> <li><code>select</code> <strong><code>as</code></strong> <code>label</code> <strong><code>for (</code></strong><code>key</code> <strong><code>,</code></strong> <code>value</code><strong><code>) in</code></strong> <code>object</code></li> <li><code>label</code> <strong><code>group by</code></strong> <code>group</code> <strong><code>for (</code></strong><code>key</code><strong><code>,</code></strong> <code>value</code><strong><code>) in</code></strong> <code>object</code></li> <li><code>select</code> <strong><code>as</code></strong> <code>label</code> <strong><code>group by</code></strong> <code>group</code> <strong><code>for</code> <code>(</code></strong><code>key</code><strong><code>,</code></strong> <code>value</code><strong><code>) in</code></strong> <code>object</code></li> </ul> </li> </ul> <p>Where:</p> <ul> <li><code>array</code> / <code>object</code>: an expression which evaluates to an array / object to iterate over.</li> <li><code>value</code>: local variable which will refer to each item in the <code>array</code> or each property value of <code>object</code> during iteration.</li> <li><code>key</code>: local variable which will refer to a property name in <code>object</code> during iteration.</li> <li><code>label</code>: The result of this expression will be the label for <code>&lt;option&gt;</code> element. The <code>expression</code> will most likely refer to the <code>value</code> variable (e.g. <code>value.propertyName</code>).</li> <li><code>select</code>: The result of this expression will be bound to the model of the parent <code>&lt;select&gt;</code> element. If not specified, <code>select</code> expression will default to <code>value</code>.</li> <li><code>group</code>: The result of this expression will be used to group options using the <code>&lt;optgroup&gt;</code> DOM element.</li> <li><code>trackexpr</code>: Used when working with an array of objects. The result of this expression will be used to identify the objects in the array. The <code>trackexpr</code> will most likely refer to the <code>value</code> variable (e.g. <code>value.propertyName</code>).</li> </ul> </div></td></tr></tbody></table></div> <h2 id="example">Example</h2> <div class="example"><div class="ng-directive-page ng-directive-select-page"><h4 id="example_source">Source</h4> <div source-edit="" source-edit-deps="angular.js script.js" source-edit-html="index.html-128" source-edit-css="" source-edit-js="script.js-127" source-edit-json="" source-edit-unit="" source-edit-scenario="scenario.js-129" source-edit-protractor=""></div> <div class="tabbable"><div class="tab-pane" title="index.html"> <pre class="prettyprint linenums" ng-set-text="index.html-128" ng-html-wrap=" angular.js script.js"></pre> <script type="text/ng-template" id="index.html-128"> <div ng-controller="MyCntrl"> <ul> <li ng-repeat="color in colors"> Name: <input ng-model="color.name"> [<a href ng-click="colors.splice($index, 1)">X</a>] </li> <li> [<a href ng-click="colors.push({})">add</a>] </li> </ul> <hr/> Color (null not allowed): <select ng-model="color" ng-options="c.name for c in colors"></select><br> Color (null allowed): <span class="nullable"> <select ng-model="color" ng-options="c.name for c in colors"> <option value="">-- choose color --</option> </select> </span><br/> Color grouped by shade: <select ng-model="color" ng-options="c.name group by c.shade for c in colors"> </select><br/> Select <a href ng-click="color={name:'not in list'}">bogus</a>.<br> <hr/> Currently selected: {{ {selected_color:color} }} <div style="border:solid 1px black; height:20px" ng-style="{'background-color':color.name}"> </div> </div> </script> </div> <div class="tab-pane" title="script.js"> <pre class="prettyprint linenums" ng-set-text="script.js-127"></pre> <script type="text/ng-template" id="script.js-127"> function MyCntrl($scope) { $scope.colors = [ {name:'black', shade:'dark'}, {name:'white', shade:'light'}, {name:'red', shade:'dark'}, {name:'blue', shade:'dark'}, {name:'yellow', shade:'light'} ]; $scope.color = $scope.colors[2]; // red } </script> </div> <div class="tab-pane" title="ngScenario e2e test"> <pre class="prettyprint linenums" ng-set-text="scenario.js-129"></pre> <script type="text/ng-template" id="scenario.js-129"> it('should check ng-options', function() { expect(binding('{selected_color:color}')).toMatch('red'); select('color').option('0'); expect(binding('{selected_color:color}')).toMatch('black'); using('.nullable').select('color').option(''); expect(binding('{selected_color:color}')).toMatch('null'); }); </script> </div> </div><h4 id="example_demo">Demo</h4> <div class="well doc-example-live animate-container" ng-embed-app="" ng-set-html="index.html-128" ng-eval-javascript="script.js-127"></div> </div></div> </div>