Created
May 15, 2016 07:52
-
-
Save johnoscott/b3919c7a28dc695c001d2cab87fac710 to your computer and use it in GitHub Desktop.
AngularJS Forms - Populate options in a SELECT form element (drop down)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('selectExample', []) | |
.controller('ExampleController', ['$scope', function($scope) { | |
$scope.colors = [ | |
{name:'black', shade:'dark'}, | |
{name:'white', shade:'light', notAnOption: true}, | |
{name:'red', shade:'dark'}, | |
{name:'blue', shade:'dark', notAnOption: true}, | |
{name:'yellow', shade:'light', notAnOption: false} | |
]; | |
$scope.myColor = $scope.colors[2]; // red | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- The proper way to do it is using the ng-options directive. | |
https://docs.angularjs.org/api/ng/directive/ngOptions | |
--> | |
<div ng-controller="ExampleController"> | |
<label>Color (null not allowed): | |
<select ng-model="myColor" ng-options="color.name for color in colors"></select> | |
</label><br/> | |
<label>Color (null allowed): | |
<span class="nullable"> | |
<select ng-model="myColor" ng-options="color.name for color in colors"> | |
<option value="">-- choose color --</option> | |
</select> | |
</span></label><br/> | |
<label>Color grouped by shade: | |
<select ng-model="myColor" ng-options="color.name group by color.shade for color in colors"> | |
</select> | |
</label><br/> | |
<label>Color grouped by shade, with some disabled: | |
<select ng-model="myColor" | |
ng-options="color.name group by color.shade disable when color.notAnOption for color in colors"> | |
</select> | |
</label><br/> | |
Currently selected: {{ {selected_color:myColor} }} | |
<div style="border:solid 1px black; height:20px" | |
ng-style="{'background-color':myColor.name}"> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment