0

I have one simple drop down here.
Here by default I need to show Tobias as selected.How to do it?

<body>

<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedName" ng-options="x for x in names">
<option selected="selected">Tobias</option>
</select>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
});
</script>

</body>

This is what am trying in my original code.

<select><option value="" selected="selected">{{data.LeaveType}}</option><option data-ng-repeat="data in leaveTypes" value="{{data.id}}">{{data.Name}}</option></select>

//Here data.LeaveType is other method(getting selected option from backend)
//data in leaveTypes -to load drop down data

1
  • Try $scope.selectedName = 'Tobias'. Commented Jun 20, 2017 at 11:05

5 Answers 5

3

Just set default value using ng-init

<select ng-model="selectedName" ng-init="selectedName=='Tobias'" ng-options="x for x in names">

DEMO

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello AngularJS</title>
</head>

<body ng-controller="myCtrl">     
<select ng-model="selectedName" ng-init="selectedName='Tobias'"
ng-options="x for x in names"></select>
 <script src="https://code.angularjs.org/1.6.1/angular.js"></script>
 </body>
</html>

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

1 Comment

post your array
2

I think leaveTypes value you are using is of object type ,where you want to display name but select id, for these kind of scenarios use ngOptions instead of ngRepeat

and modify your select as below, I had assigned id 2 for Tobias

<select ng-model="selectedName" ng-init="selectedName=2" ng-options="data.id as data.name for data in leaveTypes"></select> 

Please check below plunker for demo

https://plnkr.co/edit/GWsXGohTr6jvG67CY00D?p=preview

5 Comments

yah I have tried as you said ..am getting drop down data but that selected option only is not coming:-( trying for more than 5hrs.
Can you please send your controller and html
I got selected option srinivas :-):-) based on that selected option am displaying balance count right.for that am hitting one more method and getting response also,but am unable to append that response in available leaves field because defaultly am getting `available leaves' from service .
<form id="row editLeaveDetails" ng-repeat="data in leaveDetails"> <div> <label>Leave Type</label> <select id="levType" ng-model="selectedName" ng-init="selectedName=data.LeavetypeId" ng-options="data.id as data.Name for data in leaveTypes" ng-blur="leaveBalance(selectedName)"></select> </div> <div> <label>Availabe Leaves</label> <input id="levTaken" ng-model="data.Balance"> </div> </form>
I am getting response like this $http(httpRequest).then(function (response) { $scope.noOfValues = response.data; $scope.balanceCount = $scope.noOfValues[0].balance_count; })
1

Set default value for your ng-model object with using ng-value

<select ng-model="selectedName" ng-value="{{selectedName='Tobias'}}"
ng-options="x for x in names">

Comments

1

You can also default set selectedName in your controller:

$scope.selectedName = 'Tobias';

So in your view:

<select ng-model="selectedName" ng-options="x for x in names">

In my opinion, it is cleaner than setting it the view with ng-init, in order to keep the logic in the controller.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.selectedName = 'Tobias';
    $scope.names = ["Emil", "Tobias", "Linus"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
    <select ng-model="selectedName" ng-options="x for x in names"></select>
</div>

2 Comments

Why its not working for my original code?what I did wrong <select ng-model="selectedName" selectedName="data.LeaveType"><option data-ng-repeat="data in leaveTypes" value="{{data.id}}">{{data.Name}}</option></select>
@user7397787 Try to use ng-options.
1

You can assign your ng-model to the default value.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.names = ["Emil", "Tobias", "Linus"];
    $scope.selectedName="Tobias";
});

Working Plunker:https://plnkr.co/edit/cVaX1VdwFqAh8MoanNJS?p=preview

Comments

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.