5 Answers5

1

You can try this syntax. Also, you need to use the ng-model that will five you the value you need to select:

<select class="form-control"
        ng-model="yourmodel"
        ng-options="option for option in allEventData">
     </select>
gianni
  • 1,199
  • 2
  • 14
  • 28
1

you can set any item which should be pre-selected as value of select's ngModel

console.clear();
angular.module('so-answer', [])
  .controller('ctrl', ['$scope',
    function($scope) {
      $scope.options = [1, 2, 3, 4, 5, 6];
      $scope.selected = 3;
    }
  ])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="so-answer" ng-controller="ctrl">
  <select ng-model="selected" ng-options="opt for opt in options"></select>
</div>
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
1

In addition to the answers already posted, I'll add this one. This one demonstrates how to use an array of objects for the select. In this case the events in allEventData are assumed to have a name and value property. Adjust to your case accordingly

angular.module("app", [])
.controller("controller", function($scope){
  $scope.allEventData = [
    {
      value: 1,
      name: "Event 1"
    },
    {
      value: 2,
      name: "Event 2"
    },
    {
      value: 3,
      name: "Event 3"
    }
  ]
  
  $scope.selectedEventValue = 3
})
<!DOCTYPE html>
<html ng-app="app" ng-controller="controller">

  <head>
    <script data-require="angularjs@1.5.7" data-semver="1.5.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <select class="form-control" id="eventSelectMenu" ng-model="selectedEventValue" ng-options="item.value as item.name for item in allEventData">      
    </select>
    <pre>Selected Value: {{selectedEventValue}}</pre>
  </body>

</html>
Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41
1

You should be able to bind attribute directly like

<option ng-repeat="option in allEventData" selected={{option.selected}}>{{option.name}}</option>

similar answer

Community
  • 1
  • 1
YChi Lu
  • 79
  • 2
  • 6
1

I like this:

<select id="test"
                    name="test"
                    ng-model="vm.form.existing"
                    ng-options="item.value as item.title for item in vm.existingData">
            </select>
Dmitry Matrosov
  • 412
  • 4
  • 5