Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

NgModel modes. #126

Closed
the-spyke opened this issue Dec 19, 2013 · 15 comments
Closed

NgModel modes. #126

the-spyke opened this issue Dec 19, 2013 · 15 comments
Labels

Comments

@the-spyke
Copy link

Is there an option to set ngModel not to a selected object, but to desired object property/item id?

@BaconSoap
Copy link
Contributor

Hi, just to clarify: you're asking if when binding select2 to an array of objects, if you can make ng-model be the selected object's id?

@the-spyke
Copy link
Author

Hi, yes.

@bsr203
Copy link

bsr203 commented Dec 20, 2013

Hi I have same/similar question. same as http://stackoverflow.com/questions/14985346/setting-initial-values-of-angular-ui-select2-multiple-directive

What I expect is, if we feed a collection as options and and the initial selection (akin to selectedin normal select box) should be set to the current ng-model. I am not sure the initSelection is the right was as indicated in the SO post.

@Sharondio
Copy link

Any updates on this? I'm doing what I thought was a simple ng-model = array of selected items to initialize the select2 element, and at some point it recognizes them because the selected items are omitted from the options, but they don't show up as tags.

@Sharondio
Copy link

OK, got it working with the initSelection:

initSelection : function (element, callback) {
callback($(element).data('$ngModelController').$modelValue);
},

But I'm with bsr203, feels a bit hacky.

@fghibellini
Copy link

there is a simple_tags option that when set to true assigns the object's id instead of the object itself to the model

@sroe
Copy link

sroe commented Feb 27, 2014

@fghibellini
i can't find this option in select2 docs, where did you get that info?
I've tried to set this setting but it doesn't change anything :-(.
unbenannt
I've bound an array with ngOptions like 'item.id as item.name for item in items' and set the ngModel to the itemId which should be selected by default...

@fghibellini
Copy link

@fghibellini
Copy link

but it seems to work only when you have a multiple choice input
see #159

@sroe
Copy link

sroe commented Feb 27, 2014

@fghibellini simple_tags is as the name says only for tags-feature, which only gets in place when isSelect = false so the convertToAngularModel and convertToSelect2Model Functions getting called.
This will not fix my problem :-(.
bsr2 was having this issue with a ajax/multiple option set, so this differes from the original question by the-spike i think.
So the solution by Sharondi is also not solving my problem becaue initSelection is not called on singleselect.
On line 95 elm.select2('val', controller.$viewValue); is getting called which is (in my case) undefined -> which results in the output you can see in my post before. But I don't know why this is happening...
Edit:
Sorry line 95 is not getting called...
Edit2:
Okay I should read the readme carefully...:
Working with dynamic options
ui-select2 is incompatible with select ng-options. For the best results use option ng-repeat instead.
Sorry for wasting your time!

@dfsq
Copy link

dfsq commented Jun 1, 2014

Any update on that?

@shawn-simon
Copy link

This is really a huge pain in the ass haha

@RedJandal
Copy link

I have done a pull request which fixes this issue. ng-model binds to the id returned by select2 and it also allows you to bind a property in your controller to the select2 data object using a ui-data attribute.

Check it out and let me know what you think:
#276

@snimavat
Copy link

Any updates? we load select2 with ajax array of objects, but want objects id to be bound to model

@PowerKiKi
Copy link
Contributor

This project is deprecated. Fixes and new features are very unlikel yto happen. You should instead consider a migration to ui-select.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests