Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngOption model value 0 or false treated as 'not selected' after 1.3.0-rc.4 #9639

Closed
dwighthouse opened this issue Oct 15, 2014 · 8 comments
Closed

Comments

@dwighthouse
Copy link

Was not able to find this bug mentioned after a search.

Specific Bug Behavior

Beginning with 1.3.0-rc.5, and continuing into today's snapshot, when one of the values in an ngModel choice set is the integer 0 or the boolean false, Angular now assumes this value is equivalent to having made no selection.

This is not a 'falsy' evaluation issue. The bug manifestation does not correspond to falsy values:

  • -1 - truthy, no bug
  • 0 - falsy, bug
  • 1 - truthy, no bug
  • NaN - falsy, no bug
  • true - truthy, no bug
  • false - falsy, bug
  • undefined - falsy, no bug
  • null - falsy, no bug
  • "1" - truthy, no bug
  • "" - falsy, no bug (select correctly chooses the new option with empty string value)

Examples

The code in the following two Plunkrs is identical, except for the Angular version.

Correct behavior - Model value of empty string is reflected in the 'choose' option being visually selected at load time.
Angular 1.3.0-rc.4
http://plnkr.co/edit/OUfDk8mAneJ4pO3pwxbC?p=preview

Incorrect behavior - Model value of empty string is still stored as empty string, but the select tag visually shows the 'Zero' option being selected at load time, representing a state inconsistency between the visual display and the underlying data. Furthermore, the 'Zero' option cannot be selected directly at this point, since the tag already thinks it is selected. The user must select some other option first before returning the real selection to the 'Zero' option.
Angular 1.3.0-rc.5:
http://plnkr.co/edit/RfsAKybKJIqic4B9yBVr?p=preview

Demonstrated Need

Server-side returns list of choices in a format like so:

[
  {
    "value": 0, // or false
    "label": "Zero"
  },
  {
    "value": 1,
    "label": "One"
  },
  {
    "value": 2,
    "label": "Two"
  }
]

This format was chosen because it

  • Preserves ordering
  • Allows the server to specify the database-relevant value
  • Provides a human readable label without the client having to know every possible option in advance

Manifestation Code

<select ng-model="p.value" ng-options="c.value as c.label for c in p.choices">
  <option value="">-- choose --</option>
</select>
@jeffbcross
Copy link
Contributor

@Narretz are you working on this? I see that @petebacondarwin recently assigned it to you.

@Narretz
Copy link
Contributor

Narretz commented Nov 3, 2014

@jeffbcross I just got the assignment in the 1.x meeting, currently looking at the commit that might have caused it. It's one of those three: ab354cf, aad6095, 30996f8

I'll have a look tomorrow, but you might be able to point me in the right direction since you worked on the code.

@jeffbcross
Copy link
Contributor

It wouldn't be ab354cf since that landed in 1.3.1 (after this issue appeared). Top of my head, I'm not sure which of the other two is more likely to have introduced it.

@petebacondarwin
Copy link
Contributor

@Narretz - any update on this one?

@Narretz
Copy link
Contributor

Narretz commented Nov 6, 2014

No time yet unfortunately. I'll have another look on it later today, but if anyone else wants to take that's also okay.

----- Ursprüngliche Nachricht -----
Von: "Pete Bacon Darwin" notifications@github.com
Gesendet: ‎06.‎11.‎2014 07:33
An: "angular/angular.js" angular.js@noreply.github.com
Cc: "Narretz" mjstaffa@googlemail.com
Betreff: Re: [angular.js] ngOption model value 0 or false treated as 'notselected' after 1.3.0-rc.4 (#9639)

@Narretz - any update on this one?

Reply to this email directly or view it on GitHub.=

Narretz added a commit to Narretz/angular.js that referenced this issue Nov 6, 2014

Verified

This commit was signed with the committer’s verified signature. The key has expired.
12101111 韩朴宇
Fixes angular#9639
@Narretz
Copy link
Contributor

Narretz commented Nov 6, 2014

Fix is here: #9949

@mouryaamit
Copy link

Still the bug exist in latest version(v1.3.15)

@lgalfaso
Copy link
Contributor

lgalfaso commented Apr 4, 2015

@mouryaamit would it be possible for you to create a plunker that shows the error?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.