2014-12-23 102 views
5

問題的默認值:如何設置角度UI選

如何默認值設置爲角度UI選

下拉值是從對象中取出和對象不會被具有默認值。

和ui-select應該在前端設置默認值。

例如:

下拉值如下

1 - 所有水果 2 - 蘋果 3 -banana 4 - 水瓜

值從2至4個是從服務器發送的對象派生的,但前端需要設置默認值,即1 - 所有水果

參考以下通過ui-select2設置的示例如何在ui-select中遷移它?

<select ng-model="fID" class="select" ui-select2 data-placeholder="select fruit"> 
      <option value="">All fruits</option> 
      <option ng-repeat="f in frits value="{{f.fID}}">{{f.name}}</option> 
     </select> 

<ui-select theme="select2" ng-model="fruits.selected"> 
      <ui-select-match placeholder="select please" allow-clear="false">     {{$select.selected.name}} 
</ui-select-match> 
      <ui-select-choices repeat="f in fruits | filter:$select.search"> 
       <div ng-bind-html="f.name | highlight: $select.search:'underline'"></div> 
      </ui-select-choices> 
     </ui-select> 

http://plnkr.co/edit/a3KlK8dKH3wwiiksDSn2?p=preview https://github.com/angular-ui/ui-select 鏈接:角UI/UI選

+1

您可以在您的控制器中設置默認值/初始值 – harishr

+2

ng-init會設置一個默認值:https://docs.angularjs.org/api/ng/directive/ngInit – chapman84

回答

2

你沒有使用一個id或類似的東西的期權價值,從而UI選對象地址進行比較,如果它被選中理解。

var p1 = { name: 'Adam',  email: '[email protected]',  age: 10 }; 
    $scope.person = {selected: p1}; 
    $scope.people = [ 
    p1, 
    { name: 'Amalie', email: '[email protected]', age: 12 }, 
    { name: 'Wladimir', email: '[email protected]', age: 30 }, 
    { name: 'Samantha', email: '[email protected]', age: 31 }, 
    { name: 'Estefanía', email: 'estefaní[email protected]', age: 16 }, 
    { name: 'Natasha', email: '[email protected]', age: 54 }, 
    { name: 'Nicole', email: '[email protected]', age: 43 }, 
    { name: 'Adrian', email: '[email protected]', age: 21 } 
    ]; 

更改像這樣的plunker,你將有一個默認的選定值。

設置上視圖可以使用NG-init和作爲選擇

+1

也可以設置$ scope.person .selected = p1; – praveenpds

+0

我不明白你想說什麼? – bahadir

+0

我也沒有得到它,你只顯示了在後端做什麼,但不是指令本身應該做什麼。 – user3718908

0

設定的第一對象的默認值,我發現這個工作實施例:http://jsfiddle.net/NfPcH/28/

角代碼:

<a href="#" editable-select="user.status" e-ng-options="s.value as s.text for s in statuses"> 
    {{ showStatus() }} 
    </a> 

控制器:

app.controller('Ctrl', function($scope, $filter) { 
    $scope.user = { 
    status: 2 
    }; 

    $scope.statuses = [ 
    {value: 1, text: 'status1'}, 
    {value: 2, text: 'status2'}, 
    {value: 3, text: 'status3'}, 
    {value: 4, text: 'status4'} 
    ]; 

    $scope.showStatus = function() { 
    var selected = $filter('filter')($scope.statuses, {value: $scope.user.status}); 
    return ($scope.user.status && selected.length) ? selected[0].text : 'Not set'; 
    }; 
}); 
+0

嗨Aqib,請嘗試將代碼片段包含到您的答案中 –

+0

Stackoverflow在創建附加url時出現問題 –

+1

是的,這是情況如果您在鏈接中包含一個線路答案,現在您可以以適當的方式更新您的鏈接 –

0

在您的控制器中添加以下c ode:

$scope.fruits.selected = {name: 'All fruits'}; 

上面將設置默認選擇值爲下拉菜單。