angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Model value not properly updating on select list with ngModel and blank default option

Open chalisegrogan opened this issue 13 years ago • 9 comments

When tabbing into a select list with ng-model directive and a blank default option, the model value does not get properly updated when using arrow key to select the second option in the list. This only seems to happen on the second arrow key press after tabbing into the select list. The model is first updated on the initial selection but not on the second.

A Plunker with the repro steps and showing the issue in action is here: http://plnkr.co/edit/8bIbeUmdZfJ95lA5ssbE?p=preview

chalisegrogan avatar Oct 07 '13 18:10 chalisegrogan

The problem is that because we have removed the first "unknown" holding element, the select element does not fire the "change" event when you move down one more, since I guess, internally the selected index has not changed:

  • Tab onto select - Selected index: 0
  • Press down onto "Human": Selected index: 1 (change is fired)
  • Angular removes unknown element: Selected index: 1 but really the selected item is at index 0 (change is not fired
  • Press down onto "Cat" - Selected index: 1 (change is not fired)
  • Press down onto "Shark" - Selected index: 2 (change is fired)

petebacondarwin avatar Oct 07 '13 19:10 petebacondarwin

I wonder if we should just "hide" the unknown option rather than removing it?

petebacondarwin avatar Oct 07 '13 19:10 petebacondarwin

I am having the same issue. Is there a prefered work-around?

gusreiber avatar Apr 23 '14 17:04 gusreiber

@gusreiber I've been working around this by throwing in an additional option with style="display:none" (Plunker with example here: http://plnkr.co/edit/AX3ziM9wdSoSeLzk9ZBh?p=preview)

chalisegrogan avatar Apr 23 '14 17:04 chalisegrogan

@chalisegrogan Wow, thanks for the quick reply, Chalise. So I am actually trying to use ng-options in my select list, specifically, "opt.id as opt.label for opt in data.valuesEnum". When my initial value is undefined, the first change from null to data.valuesEnum[0] my model updates and ng-change fires and all is good. When I change from the first option to the second, the model does not update and no ng-change event fires. After that, everything works as expected.

gusreiber avatar Apr 23 '14 18:04 gusreiber

@gusreiber - I believe this (http://plnkr.co/edit/gYpCMsSTdLBDkxulXu5x?p=preview) should work for you..?

chalisegrogan avatar Apr 23 '14 19:04 chalisegrogan

Ah. Perfect. ...what you said the first time, really. I did not realize you could use both ng-options and

This seems like a fairly significant bug in the select directive to me, but your initial post is over 6 months old. No one is banging down the door looking for a real fix?

...anyway, thx again.

gusreiber avatar Apr 23 '14 20:04 gusreiber

@gusreiber agreed, I've been meaning to get in a PR for it myself but just keep getting sidetracked. Glad I could help!

chalisegrogan avatar Apr 23 '14 20:04 chalisegrogan

Duplicate of https://github.com/angular/angular.js/issues/9134

Narretz avatar Sep 22 '15 11:09 Narretz