Search code examples
javascriptangularjsangularjs-ng-repeatng-options

angular ng-options with preselected value by object id


Is there a way the following select can have a pre-selected club ?

<select ng-model="c.pilot.clubIdx" ng-options="club.idx as club.name for club in c.clubs track by club.idx">

c.pilot.clubIdx = 2
c.clubs = { {idx: 1, name: "club1"}, {idx: 2, name: "club2"} }

The select-menu should show "club2".

Since the clubs are stored in a db and can be altered, I like to keep them only as references on the pilots.

SOLVED: removing track by solves the problem.


Solution

  • in the controller set the ng-model of the select to the value of the option you want selected. Something like : $scope.c.pilot.clubIdx = 2 or $scope.c.pilot.clubIdx = $scope.c.clubs[1].idx