admin管理员组

文章数量:1291205

Question :

how to set default value to the angular ui-select

drop down values are fetched from object and object wont be having default value.

and ui-select should set the default value in Frontend.

eg:

drop down values are as follows

1 -all fruits 2 -apple 3 -banana 4 -water melon

value from 2 to 4 are derived from object sent from server.but Frontend need to set default value ie 1 - all fruits

Referring to the below example set via ui-select2 how to migrate this in 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>

Link :angular-ui/ui-select

Question :

how to set default value to the angular ui-select

drop down values are fetched from object and object wont be having default value.

and ui-select should set the default value in Frontend.

eg:

drop down values are as follows

1 -all fruits 2 -apple 3 -banana 4 -water melon

value from 2 to 4 are derived from object sent from server.but Frontend need to set default value ie 1 - all fruits

Referring to the below example set via ui-select2 how to migrate this in 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./angular-ui/ui-select Link :angular-ui/ui-select

Share Improve this question asked Dec 23, 2014 at 15:38 praveenpdspraveenpds 2,9365 gold badges27 silver badges44 bronze badges 2
  • 1 you can set the default/initial value in your controller – harishr Commented Dec 23, 2014 at 15:41
  • 2 ng-init will set a default value: docs.angularjs/api/ng/directive/ngInit – Cameron Chapman Commented Dec 23, 2014 at 19:49
Add a ment  | 

3 Answers 3

Reset to default 2

you didnt use an id or something like that for option value so ui-select pares object address to understand if it is selected.

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 }
  ];

change plunker like this and you will have a default selected value.

to set the default value on view you can use ng-init and set first object as selected

In your controller add the following code:

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

The above will set the default selected value to the drop down.

I found this working example: http://jsfiddle/NfPcH/28/

Angular code:

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

Controller:

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';
  };
});

本文标签: javascripthow to set a default value for angular uiselectStack Overflow