1

I have this html which works very well. My topicId displays from 0 to 7 and 0 is the first item. However nothing is displayed as the default in my dropdown when the page loads until I click the dropdown.

I will like the first item to be displayed as the default. How can I achieve this please?

        <div class="selectTopic">
            <select id="cTopic" name="cTopic" ng-model="selectedValue">
                <option ng-repeat="x in cuc.getData" value="{{x.topicId}}"> {{x.topicDescription}}</option> 
            </select>
        </div>
2
  • Try to set your model (selectedValue) to the first item in your option's ng-repeat collection. Commented May 1, 2017 at 20:46
  • How ? Do you have a code sample? Commented May 1, 2017 at 20:48

3 Answers 3

0

You should use ngOptions instead. Then init the model via the controller to the default value:

<select id="cTopic" name="cTopic" ng-model="selectedValue" 
    ng-options="item.topicId as item.topicDescription for item in cuc.getData">
</select>

And in controller:

$scope.selectedValue = "topicId1";

See example:

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

app.controller("testCtrl", function ($scope) {
	
  $scope.selectedValue = "topicId1";
  $scope.items = [
    {
      topicId : "topicId1",
      topicDescription :"topicDescription1"
    },
    {
      topicId : "topicId2",
      topicDescription : "topicDescription2"
    },
    {
      topicId : "topicId3",
      topicDescription  : "topicDescription3"
    }
  ];
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="TestApp" ng-controller="testCtrl">
    <div class="selectTopic">
        <select id="cTopic" name="cTopic" ng-model="selectedValue" ng-options="item.topicId as item.topicDescription for item in items">
        </select>
    </div>
</div>

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

Comments

0

You need use ng-selected: https://docs.angularjs.org/api/ng/directive/ngSelected

 <div class="selectTopic">
    <select id="cTopic" name="cTopic" ng-model="selectedValue">
        <option ng-repeat="x in cuc.getData" value="{{x.topicId}}" 
            ng-selected="selectedValue === x.topicId"> 
            {{x.topicDescription}}
        </option> 
    </select>
 </div>

However, if you are using plain html, AngularJS has more efficient ways to create dropdowns, see: https://docs.angularjs.org/api/ng/directive/ngOptions

Comments

0

The suggested answer will work too, however an alternative that I tend to use is this:

<div class="selectTopic">
<select id="cTopic" name="cTopic" ng-model="selectedValue" ng-options="x.topicId as x.topicDescription for x in cuc.getData">
    <option value="">Select One ...</option> 
</select>

Angular has to start with some value for un-initialized selects, so providing a blank option in the html safely initializes the select without making assumptions on behalf of your users.

4 Comments

Am working on an application that will be in different languages so I cant have "Select One" in code. Everything is coming from the database
I'm not sure what you mean? the value that will be stored in the model is an empty string (if you don't select something else).
I tried it and no luck. All other items in the dropdown are not displaying again just the first item is displaying. Select One ..
oh i missed some stuff in the ng-options, let me edit

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.