0

I would like to have the first element of my ng-options to be the default one:

<select data-ng-options='item.pk for item in items' data-ng-click='update()'></select>

(so this is different than having an <option value=''>Default</option> at the end of the select. I also would like update() to be called at the beginning (on the default option). How can I achieve this ?

dcodesmith
  • 9,590
  • 4
  • 36
  • 40
Scipion
  • 11,449
  • 19
  • 74
  • 139
  • possible duplicate of [how to use ng-option to set default value of select element](http://stackoverflow.com/questions/17329495/how-to-use-ng-option-to-set-default-value-of-select-element) – dting May 06 '15 at 15:46

1 Answers1

1

First you want to create a model for the select element:

<select ng-model="selectedItem" ng-options="item.pk for item in items"
     ng-click="update()">
</select>

After that, in your controller, you want to set selectedItem to the first element in items, and call your update function:

$scope.selectedItem = $scope.items[0]; $scope.update();

Notice that you should define update before calling it.

Tom Arad
  • 251
  • 2
  • 7