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

Select2 triggers onchange when init #153

Closed
ghost opened this issue Feb 10, 2014 · 4 comments
Closed

Select2 triggers onchange when init #153

ghost opened this issue Feb 10, 2014 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 10, 2014

When the page loads, the ui.select2 will trigger a change event even though the select wasn't touched. It seems to be this line's culprit: https://github.com/angular-ui/ui-select2/blob/v0.0.5/src/select2.js#L127

What does this do ?The comment above it doesn't really help ...

@MilosMosovsky
Copy link
Contributor

check this #173

@mattcasey
Copy link

I don't know about #173 or related issues, I think this is slightly different. There are several variables in play that makes a single solution difficult. (for example, whether you're using a SELECT tag or not, the code does different things - I am using a DIV because I want to use tags). I have two solutions to prevent the onchange event on init:

  1. Change source code on line 210*:
controller.$setViewValue(
  convertToAngularModel(elm.select2('data'))
);

to:

controller.$setViewValue(controller.$modelValue);
  1. (I'm doing this, so I don't need to mess with source code): Set options.initSelection to "true" or "function(){}" (either one doesn't seem to cause an error for me) so ui-select2 doesn't trigger the controller.$setViewValue.

* I don't understand why controller.$setViewValue() is even needed. It's being passed an array of values from the same array inside controller.$modelValue, but it's a new array because first it's translated by select2, than re-translated by convertToAngularModel, hence $setViewValue() triggers the onchange event.

@sazo
Copy link

sazo commented Jan 10, 2015

+1 for this!

@PowerKiKi
Copy link
Contributor

ui-select2 is deprecated, you should use https://github.com/angular-ui/ui-select instead. It is very unlikely that this issue will be fixed.

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

No branches or pull requests

4 participants