0

What is the easiest way to select default value of select box, by using angularJs?

I have this control:

    <div class="col-md-1">
        <label for="timeZone">Time zone</label>
        <select id="timeZone" name="timeZone" class="form-control"
            ng-model="newTestSessionCtrl.formData.timeZoneValues"
            ng-options="timeZone.name for timeZone in newTestSessionCtrl.viewData.timeZoneValues">
            <option value="" disabled selected>Select</option>
        </select>
    </div>

And in my controller I have definde timeZoneValues like this:

timeZoneValues: [{value: "UTC", name: "UTC"}, {value: "UTC+1", name:"UTC+1"}]

What I was trying to do is to use ng-init. Like it is explained here but I guess I am doing something the wrong way.

What I am trying to do is to select UTC by default.

Community
  • 1
  • 1
nemo_87
  • 4,523
  • 16
  • 56
  • 102

5 Answers5

0

is this work?

<div class="col-md-1">
    <label for="timeZone">Time zone</label>
    <select id="timeZone" name="timeZone" class="form-control"
        ng-model="newTestSessionCtrl.formData.timeZoneValues"
        ng-init="newTestSessionCtrl.formData.timeZoneValues = timeZone[0]"
        ng-options="timeZone.name for timeZone in newTestSessionCtrl.viewData.timeZoneValues">
        <option value="" disabled selected>Select</option>
    </select>
</div>
Eren Akkus
  • 471
  • 4
  • 11
0

Remove the empty option:

<div class="col-md-1">
    <label for="timeZone">Time zone</label>
    <select id="timeZone" name="timeZone" class="form-control"
        ng-model="selected"
        ng-options="timeZone as timeZone.name for timeZone in newTestSessionCtrl.viewData.timeZoneValues">
    </select>
</div>

and on the javascript add

$scope.selected = timeZoneValues[0].name;

Beslinda N.
  • 4,808
  • 5
  • 27
  • 33
0

I had the same problem and this is how I solved:

Here is the html code:

<div class="col-md-1">
    <label for="timeZone">Time zone</label>
    <select ng-model="defaultValue" ng-options="timeZone.name for timeZone in newTestSessionCtrl.viewData.timeZoneValues" ></select>
</div>

And in the controller I set the default value to the first value of the array:

$scope.defaultValue = $scope.timeZoneValues[0].name;

Hope this helps

aurel
  • 54
  • 1
  • 11
  • That's incorrect, your use of ng-model is invalid as it would bind the selected option to the array, overwriting the original value available in position 0 once you have changed it. – pedromarce Oct 27 '16 at 13:04
  • @pedromarce That has worked for me, and there was no overwriting of the original value – aurel Oct 27 '16 at 13:06
  • Try to select the second option in your select, has the first option now been changed to be like second? – pedromarce Oct 27 '16 at 13:10
  • By the way, your first code is correct, problem is in your second option (the one untested). – pedromarce Oct 27 '16 at 13:16
  • @pedromarce I see, Ok then I cannot argue with that, I will remove that one then – aurel Oct 27 '16 at 13:19
0

Assign the default value in your controller to the ng-model:

timeZoneValues: [{value: "UTC", name: "UTC"}, {value: "UTC+1", name:"UTC+1"}];
newTestSessionCtrl.formData.timeZoneValues = "UTC";

newSessionCtrl needs to be whatever you are using to refer to the controller itself from the controller, or scope if you are not.

Also, you could use:

<div class="col-md-1">
    <label for="timeZone">Time zone</label>
    <select id="timeZone" name="timeZone" class="form-control"
        ng-model="newTestSessionCtrl.formData.timeZoneValues"
        ng-options="timeZone.name for timeZone in newTestSessionCtrl.viewData.timeZoneValues"
        ng-init="newTestSessionCtrl.formData.timeZoneValues = newTestSessionCtrl.viewData.timeZoneValues[0].name">
    </select>
</div>
pedromarce
  • 5,651
  • 2
  • 27
  • 27
0

You have missed assigning value inside ng-init directive in the select tag. See the below working plnkr

http://plnkr.co/edit/SBrSUjrfWHJvkufVjSJG?p=preview

HTML:

 <body ng-controller="MainCtrl">
    <div class="col-md-1">
        <label for="timeZone">Time zone</label>
        <select id="timeZone" name="timeZone" class="form-control" ng-init="timeZoneModel = timeZoneValues[0]"
            ng-model="timeZoneModel"
            ng-options="timeZone.name for timeZone in timeZoneValues">
            <option value="" disabled selected>Select</option>
        </select>
    </div>
  </body>

JS:

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

app.controller('MainCtrl', function($scope) {
  $scope.timeZoneValues = [{value: "UTC", name: "UTC"}, {value: "UTC+1", name:"UTC+1"}];
});