question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ng-model bug: Options shifted by one after selecting the first time

See original GitHub issue

If you use the ng-model attribute, all the data-option-array-index attributes for li the elements under the chosen-results list are shifted by one (1, 2, 3… instead of 0, 1, 2…). Selection works the first time – I’m guessing because the placeholder option is in the array and shifting all the other elements – but every subsequent selection actually assigns the value after the selected value to the model (click on the second option, and the model is assigned the value for the third option). Picking the last value causes JQuery to throw an error.

How to reproduce: Open angular-chosen/example/index.html Pick a value from “Chosen with static options” Pick another one and watch the model update to the value after it (or if the value selected is the last one, see JQuery throw an error in the console)

I’ll try to see if I can fix it.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
rhettlcommented, Aug 22, 2013

I don’t know the exact cause, but I figured out a fix for it. and I cannot believe how dumb this is:

<select style="width: 100%" data-chosen data-disable-search="true"
            data-ng-model="product"
            data-ng-options="prod.guid for prod in products">
        <option><option>
</select>

I started thinking the issue may be the null li that is created when $scope.product has no value during init. So I figured that if I gave it a value before loading chosen that it would fix the issue. I tried this and it worked! I was astounded I though I would at least need to add seleced="selected" to the option. If this didn’t work I was going to add ng-init="product = products[0]"

2reactions
harunlegozcommented, Oct 20, 2015

I think that this issue is covered in the recent documentation. Just add an empty valued option element:

https://github.com/localytics/angular-chosen

Also important: if your ngModel is null or undefined, you must manually include an empty option inside your <select>, otherwise you’ll encounter strange off-by-one errors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 - [(ngModel)] not updating after [value] changes
1. The ngModelChange thing doesn't work either. It's really weird because the value in the input GETS UPDATED, but not the expression {{product....
Read more >
Difference between (change) and (ngModelChange) in Angular
In this post we're going to cover the difference between (change) and (ngModelChange) events with an <input> inside an Angular component.
Read more >
Built-in directives - Angular
NgModel, Adds two-way data binding to an HTML form element. ... For this use case, Angular applies the styles upon initialization and in...
Read more >
ngModel.NgModelController - AngularJS: API
A model is considered to be touched when the user has first focused the control ... If you have an input that uses...
Read more >
Select control does not fire change detection - Mobiscroll
This means, that the select can be empty only when the data array is also empty. Otherwise the select value always has to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found