In AngularJS, the ng-options directive allows you to dynamically generate
To generate options from an array, use the following syntax:
<select ng-model="selectedValue" ng-options="item.value as item.label for item in items">
where:
Consider the following AngularJS controller:
$scope.items = [ {id: '000001', title: 'Chicago'}, {id: '000002', title: 'New York'}, {id: '000003', title: 'Washington'} ];
To render this data as a
<select ng-model="selectedItem" ng-options="item.id as item.title for item in items">
To select a specific option by default, use the ng-init directive:
<select ng-model="selectedItem" ng-init="selectedItem = items[1]" ng-options="item.id as item.title for item in items">
By adding a custom
<select ng-model="selectedItem"> <option value="">Select One</option> <option ng-repeat="item in items" ng-value="item.id">{{item.title}}</option> </select>
To hide the custom default option after a selection is made, use ng-hide:
<select ng-model="selectedItem"> <option value="" ng-hide="selectedItem">Select One</option> <option ng-repeat="item in items" ng-value="item.id">{{item.title}}</option> </select>
The above is the detailed content of How do you create dynamic dropdown lists in AngularJS using ng-options?. For more information, please follow other related articles on the PHP Chinese website!