问题
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.
And here is the screenshot of UI
回答1:
Take out the disabled attribute in your first option.
回答2:
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"
回答3:
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>
来源:https://stackoverflow.com/questions/41796338/angular-ng-select-does-not-show-ng-selected-value