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

fix(select): prevent unknown option being added to select when bound … #11875

Closed
wants to merge 1 commit into from

Conversation

divercraig
Copy link
Contributor

…to null property

If a select directive was bound, using ng-model, to a property with a value of null this would
result in an unknown option being added to the select element with the value "? object:null ?".
This change prevents a null value from adding an unknown option meaning that the extra option is
not added as a child of the select element

Closes #11872

…to null property

If a select directive was bound, using ng-model, to a property with a value of null this would
result in an unknown option being added to the select element with the value "? object:null ?".
This change prevents a null value from adding an unknown option meaning that the extra option is
not added as a child of the select element

Closes angular#11872
@caitp
Copy link
Contributor

caitp commented May 14, 2015

I'm not sure this is right. On the surface, it looks okay, but the thing is, the unknown options are there for a reason (which is, when the model value is defined but doesn't match one of the specified options). I think this probably behaves as expected if the value is non-null, but it's inconsistent with the way ngModel normally works.

@divercraig
Copy link
Contributor Author

I don't really think unknown options should be added for null model values. The value hasn't been defined if it's null.
I haven't changed the implementation of unknown options which are non-null, only null options.

@caitp
Copy link
Contributor

caitp commented May 14, 2015

Yes, but this still violates the norm of ngModel, I'm not positive we want to do that. "null" does not mean "undefined", it means "null"

@divercraig
Copy link
Contributor Author

urgh, this has failed a test due to an (i believe) unrelated timeout.
How can I resubmit for travis testing without closing the pull request and creating a new one?

@caitp
Copy link
Contributor

caitp commented May 14, 2015

@petebacondarwin what do you think, break the ngModel norm here or no?

@divercraig
Copy link
Contributor Author

This change is trying to fix a problem that was introduced between 1.2.23 and now. I guess it's a regression.

@petebacondarwin
Copy link
Member

I was also worried about this change; The question is whether null can be a valid value or not. I think I was even the one who wrote the test that explicitly checks that null triggers the unknown option. At the time I assumed that we should consider null a valid value, as @caitp points out.

Looking at the code further I realise now that select actually sets the model to empty string if the user actually selects the "empty" option. Moreover, when we are talking about non-ngOptions based select directives then the values for the options can only be strings, so we can never match null against any of the options.

Here is a bit of a clearer plunker with regards the types of the models: http://plnkr.co/edit/Vc5ngCtdhzWwsQXcscKC?p=preview

So I think we should merge this PR as in the case of a select rather than an ngOptions directive, null is indistinguishable from undefined (or empty string) for that matter.

Can anyone spot a pain point in current applications from merging this? @caitp, @Narretz, @divercraig

@petebacondarwin
Copy link
Member

@divercraig - I just noticed that this behaviour has been the case even before 1.2.23. See http://plnkr.co/edit/Vc5ngCtdhzWwsQXcscKC?p=preview
So I am not sure how it is a regression.

@petebacondarwin petebacondarwin modified the milestones: 1.4.0-rc.3, Purgatory, 1.4.x - jaracimrman-existence May 18, 2015
@caitp
Copy link
Contributor

caitp commented May 18, 2015

personally I think it's working as expected, but I don't care a whole lot if it is merged

@petebacondarwin
Copy link
Member

@divercraig - Here is a simple workaround that doesn't require us to make a potentially breaking change to the core, and which works for all recent versions of Angular 1.2, 1.3 and 1.4.

http://plnkr.co/edit/6y0eMiLq6VFVqZLPnVui?p=preview

@divercraig
Copy link
Contributor Author

@petebacondarwin I can see your "issue" in 1.2.23 which is weird because I only started noticing this problem in my project when I upgraded to 1.3.15 from 1.2.23.

I acknowledge that there is a workaround, but I think if we think about it this change makes sense and is safe:

  • Why would anyone expect the current behaviour (the addition of a new empty option with a value like ? object:null ?)
  • null and undefined and empty string are all different things it's true... but in the context of an option element we cannot represent null or undefined and as such use empty string. So in the context of an option in html null, undefined and empty string are treated as the same thing.

@petebacondarwin
Copy link
Member

@divercraig - OK. I agree with you. The current behaviour, whether it is "correct" or not, is confusing and surprising. I'll land this PR this week.

@petebacondarwin petebacondarwin self-assigned this May 18, 2015
petebacondarwin added a commit that referenced this pull request May 18, 2015
…to null property

If a select directive was bound, using ng-model, to a property with a value of null this would
result in an unknown option being added to the select element with the value "? object:null ?".
This change prevents a null value from adding an unknown option meaning that the extra option is
not added as a child of the select element.

Since select (without ngOptions) can only have string value options then `null` was never a
viable option value, so this is not a breaking change.

Closes #11872
Closes #11875
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
…to null property

If a select directive was bound, using ng-model, to a property with a value of null this would
result in an unknown option being added to the select element with the value "? object:null ?".
This change prevents a null value from adding an unknown option meaning that the extra option is
not added as a child of the select element.

Since select (without ngOptions) can only have string value options then `null` was never a
viable option value, so this is not a breaking change.

Closes angular#11872
Closes angular#11875
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Select elements bound to a scope property with a value of null will add an extra option
5 participants