2

I have a drop-down list that I'm trying to create where the value displayed in the drop-down is different to the options available - I'm using angularjs for this purpose. For example, if I had the following text, I'd want to display the full value when the user opens the drop-down:

A - A is for Apple

B - B is for Banana

But on the page I only want to display A or B, not the full description, as in this picture: enter image description here

This is the closest I've got so far (where my list of objects are just objects with a Value and Description property) but I can't seem to show the short value in the dropdown, although I know I've seen this kind of set up online on various sites.

<select>
 <option ng-repeat="item in myObject.Options" value="{{item.Value}}" title="{{item.Description}}">{{item.Description}}</option>
</select>

Where the object would look something like

var myObject = { "Options" : [
{ "Value":"A" , "Description":"A is for Apple" },
{ "Value":"B" , "Description":"B is for Banana" },
{ "Value":"C" , "Description":"C is for Cherry" } ]};
2
  • 2
    Post the sample data of myObject.Options. Commented Jul 31, 2017 at 13:07
  • 1
    @PrerakSola done! Hope that's acceptable. Commented Jul 31, 2017 at 13:14

2 Answers 2

2

This is not possible, because you have uses the HTML select component that is not able to distinguish between the displayed text in the dropdown and the shown value.

However you could build an own directive for implementing a similar behaviour, for example with a bootstrap dropdown menu or md-select.

For example :

 <md-select md-selected-text="selectedItem.Value" ng-model="selectedItem">
      <md-option ng-repeat="item in items" ng-value="item">
         {{ item.Description }}
     </md-option>
 </md-select>

Hope this will help you !

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed to know - I wasn't sure if it was possible so thanks for clearing this up and I will use a method like this to create the functionality.
0

If you only want to display the Value part, you can use this:

<select>
    <option ng-repeat="item in myObject.Options" value="{{item.Value}}" title="{{item.Description}}">
        {{item.Value}} - {{item.Description}}
    </option>
</select>

Example:

var app = angular.module('app', []);

app.controller('TestController', function($scope) {

  $scope.myObject = {
    "Options": [{
      "Value": "A",
      "Description": "A is for Apple"
    }, {
      "Value": "B",
      "Description": "B is for Banana"
    }, {
      "Value": "C",
      "Description": "C is for Cherry"
    }]
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller='TestController'>
  <select>
    <option ng-repeat="item in myObject.Options" value="{{item.Value}}" title="{{item.Description}}">{{item.Value}} - {{item.Description}} </option>
  </select>
</div>

1 Comment

That just displays the values in the dropdown, not a combination of values and descriptions as requested in the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.