Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there a easy way to have options as objects in array? Like following:

$scope.search.categories = [{id: '1',name: 'first'},{id: '2',name: 'last'}];

<select ui-select2 ng-model="search.categories" multiple style="width:300px" data-placeholder="select category">
  <option ng-repeat="category in search.categories" value="{{category}}">{{category.name}}</option>

</select>

Or am I totally wrong?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Checkout this demo I've put together: http://plnkr.co/edit/gist:4279651?p=preview

If you want the selected item to be an object use <input>

The demo shows you 4 different ways to do uiSelect2

share|improve this answer
 
Is there any way to do this declaratively? –  CWSpear Aug 12 at 22:13
add comment

Yes, use the ng-options and the ng-model like this:

<select ui-select2 ng-model="selectedCategory" ng-options="c.name for c in search.categories">
  <option value="">Pick a category</option>
</select>

Note: $scope.selectedCategory will hold the reference to the selected category object.

share|improve this answer
1  
thank you! will try it out, but how about info at angular-ui.github.com saying: ui-select2 is incompatible with <select ng-options>. For the best results use <option ng-repeat> instead What is your experience? Is it safe? –  Jakub Kuchar Jan 15 at 12:07
 
if the docs say it's incompatible don't use ng-options. –  Scott Silvi Jul 8 at 18:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.