Angularjs – Angular UI / select2 multiple how to preselect values/options

angular-uiangularjsjquery-select2

using angular-ui with select2 as follows:

<select ui-select2 ng-model="search.categories" multiple style="width:300px" data-placeholder="select category">
      <option value="open" >open</option>
      <option value="close" >close</option>
</select>

Where and how should I preselect options? By default selected option is only first.Somehow in controller?

Best Answer

For a simple <select> list it's easy:

MyController function($scope) {
    $scope.search = {
        categories: 'close'
    };
}

For <input> it gets trickier because you may need to add a initSelection option