2016-12-01 46 views
0

我使用ui-select元素在Angular中編寫了一個小程序。下面是HTML代碼:如何在ui-select中設置角度的默認值?

<ui-select ng-model="test.selectedContract"> 
    <ui-select-match > 
     {{$select.selected.name}} - {{$select.selected.value}} ---- {{$select.selected.id.id}} *** {{$select.selected.policy.info.name }}  
    </ui-select-match> 

    <ui-select-choices group-by="groupByLetter" 
    repeat="contract in (contracts | 
    orSearchFilter: {id.id: $select.search, policy.info.name : $select.search} | 
    orderBy: 'name') track by contract.name"> 
      {{contract.name}} - {{contract.value}} ---- {{contract.id.id}} *** {{contract.policy.info.name }} 
    </ui-select-choices> 
    </ui-select> 

這裏是我的合同結構對象:

$scope.contracts = [{ 
     name: "contract1.00", 
     value: 10, 
     id: { 
     id: 8000, 
     code: 2 
     }, 
     policy: { 
     info: { 
      name: "test1", 
      country: "test" 
     } 
     } 
    }, //other contract objects 

最後,還有我的plunker代碼:http://plnkr.co/edit/PigjGCj5ukxscJC7Wl97?p=preview

的問題是,我想顯示「全部」作爲默認值(如果沒有選擇),而我的默認值在我的plunker是:「 - ---- ***」

你能幫我設定這個默認值嗎? 謝謝!

回答

0

您可以在控制器中定義一種toString函數,並將結果放在ui-select標記中,而不是硬代碼破折號和星號。如果值爲空,它應該返回 「全部」

0

只需設置

$scope.test.selectedContract = { 
     name: "all", 
     value: 10, 
     id: { 
     id: 8000, 
     code: 2 
     }, 
     policy: { 
     info: { 
      name: "test1", 
      country: "test" 
     } 
     } 
    } 

DEMO