Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-repeat setting default select value

$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

like image 592
ngplayground Avatar asked Jul 21 '26 04:07

ngplayground


1 Answers

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];
like image 123
Davin Tryon Avatar answered Jul 22 '26 19:07

Davin Tryon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!