3
$scope.activities =
    [
        { id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
        { id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
    ];

<select data-ng-model="engineer.currentActivity" data-ng-options="a.name for a in activities">                
</select>

Using the above I am able to create a select box with 3 values, a blank one and then dropdown menu and horizontal bar.

I would like to set Horizontal Bar as my default and cannot see how to do this.

Help would be great

1
  • 1
    BTW, if you are going to end up pulling activities from the server, and you need to hydrate the selected option, you want to look into track by expressions inside the ng-options expression. Commented Jan 25, 2014 at 15:19

3 Answers 3

4

In the controller, just add a line to setup the initial selection:

$scope.activities =
    [
        { id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
        { id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
    ];

$scope.engineer = {}; // if needed
$scope.engineer.currentActivity = $scope.activities[1];
Sign up to request clarification or add additional context in comments.

Comments

3

In your angular controller:

$scope.activities = [
    { id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
    { id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
];
$scope.activity = $scope.activities[1]; //Bind 2nd activity to the model.

In the HTML:

<select ng-model="activity" 
    ng-options="activity as activity.name for activity in activities">
    <option value=""></option>
</select>

See the Fiddle

Comments

0

Use ng-init directive

ng-init="engineer.currentActivity = options[1]"

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.