Angular : ng-select does not show ng-selected value [duplicate]

陌路散爱 提交于 2020-01-07 09:15:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!