2017-01-22 30 views
0

嘗試在select中根據id設置默認值。Angular:ng-select不顯示ng-selected值

我有一個對象location其中<pre>{{location.routeId}}</pre>打印一個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> 

appData.Routesroute有參數nameid,我想匹配route.idlocation.routeId

如果我檢查元素,對於其中一個選項,我可以看到ng-selected爲true,但仍然未在UI中選擇它。

enter image description here

這裏是UI

enter image description here

回答

0

的 截圖拿出你的第一個選項禁用屬性。

+0

沒有幫助。 –

0

試試這個:

<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> 
+0

基本上我需要返回具有'name'和'id'的完整'route'對象。有什麼辦法可以做到嗎? –

+0

@DeepakBandi檢查更新 –

+0

它現在選擇了第一個選項。 ''我如何選擇正確的ID? –

0

你並不需要ngSelected指令花括號。所以,簡單地將其刪除:

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

要:

ng-selected="route.id == location.routeId" 
+0

這並沒有幫助。 –

+0

你可以創建一個plunker嗎? @DeepakBandi – Yaser