-1

Trying to set the default value in select depending on the id.

I have a object location where <pre>{{location.routeId}}</pre> prints an id 819e41ef-0081-41e6-a415-2766dc16e8e4

<select ng-model="location.route">
        <option value="" disabled>Choose Route</option>
        <option ng-repeat="route in appData.Routes" 
             ng-selected="{{route.id == location.routeId}}" 
             value="{{route}}">
             {{route.id}} : {{route.name}}
        </option>
</select>

The route inappData.Routes has parameters name and id, I'm trying to match route.id with location.routeId

If I inspect element, I can seeng-selected as true for one of the options, but still its not being selected in the UI.

enter image description here

And here is the screenshot of UI

enter image description here

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Deepak Bandi
  • 1,854
  • 4
  • 21
  • 37

3 Answers3

-1

Take out the disabled attribute in your first option.

Brendan Long
  • 276
  • 1
  • 6
-1

Try this:

<select ng-model="location.route">
        <option value="">Choose Route</option>
        <option ng-repeat="route in appData.Routes" 
             ng-selected="route.id == location.routeId || location.route && route.id == location.route.id" 
             ng-value="route">
             {{route.id}} : {{route.name}}
        </option>
</select>
MeTe-30
  • 2,512
  • 2
  • 21
  • 30
-1

You don't need curly braces for ngSelected directive. So simply remove them:

ng-selected="{{route.id == location.routeId}}"

To:

ng-selected="route.id == location.routeId" 
Yaser
  • 5,609
  • 1
  • 15
  • 27