beginner
beginner

Reputation: 303

using ng-option to display dropdown values

In my application, I have been using options for the scroll down menu. In place of this, I want to use ng-option such that the values come from javascript file. I even need help with angular js code for this. Here is my HTML code with options values. I need some help.

<div class="form-group">
    <label class="control-label col-lg-2 pull-left">Quality<span class="Imp">*</span></label>
    <div class="col-lg-8">
        <select id="Quality" name="Quality" class="form-control" style="width:170px" ng-model="vm.EditRef_UI.Quality"
            tooltip="Quality is required" tooltip-placement="top" required>

            <option selected value="Satisfactory">Satisfactory</option>
            <option value="NotSatisfactory">Not Satisfactory</option>
        </select>
    </div>
</div>

Upvotes: 2

Views: 1488

Answers (3)

Abdullah Al Noman
Abdullah Al Noman

Reputation: 2878

 <select ng-options="category.value as category.name for category in sourceValues"  ng-model="Quality"></select>

1st input category.value will be the value of option and category.name would be the value shown on the drop-down list

In the controller define a array with option and their value that you want to use

$scope.sourceValues = [
       {value: 'Satisfactory', name: 'Satisfactory'},
       {value: 'NotSatisfactory', name: 'Not satisfactory'}

      ];

Upvotes: 5

Cristian Colorado
Cristian Colorado

Reputation: 2040

For instance if you have populated your quality options into a scope variable 'qualities':

$scope.qualities = [{id: 1, label: 'Low Quality'}, {id: 2, label: 'Medium Quality'}, {id: 3, label: 'High Quality'}];

You can update your html like:

<div class="form-group">
      <label class="control-label col-lg-2 pull-left">Quality<span class="Imp">*</span></label>
          <div class="col-lg-8">
               <select id="Quality" name="Quality" class="form-control" style="width:170px" ng-options="item as item.label for item in qualities track by item.id" ng-model="vm.EditRef_UI.Quality"  tooltip="Quality is required"                                                                    tooltip-placement="top" required>
                </select>
           </div>

You would need to use attribute ng-options like this:

ng-options="item as item.label for item in qualities track by item.id"

Your selection will still be updated on scope variable vm.EditRef_UI.Quality.

Upvotes: 1

Kamal
Kamal

Reputation: 161

Based on the documentation you could do the following.

<select ng-options="item as item.label for item in qualities track by item.id" ng-model="Quality"></select>

Upvotes: 1

Related Questions