-
Notifications
You must be signed in to change notification settings - Fork 27.5k
ng-options is evaluated before ng-if causing invalid "null" option #8408
Comments
This would happen because the select element is higher up the DOM tree than the null option with the ng-if, so it would compile first. I think the appropriate solution would be to move the functionality for optional null options into the select directive instead of using ng-if on the null options, perhaps with an optional attribute. |
@wesleycho what do you think about adding the optional attribute Proposed accepted values for
This would match the functionality provided by Ruby On Rail's select tags |
I don't think that this is a use case we want to support. Why not: <select ng-model="ctrl.color" ng-options="color.name for color in ctrl.colors">
<option value="">{{nullLabel}}</option>
</select> and then set |
@btford that works for my first 2 use cases, but does not work for the 3rd requirement of not having a null option, which is what promoted me to open this ticket. Currently I'm using this stackoverflow answer to get the functionality I'm looking for. Here is the plunker of it in action. I don't think my need for an optional null value is unusual. While its true that this functionality can easily be replicated in a linking function - I think adding my proposed |
While I'm sympathetic towards wanting a simple way to implement this behavior, I really don't see it happening for the following reasons:
|
+10 on wanting this feature!!! |
I agree with Brian that this is not something we want. Although I think that starting from 1.4.7, you can use one option with |
The example @Narretz pasted has a problem that the entire items are listed twice. |
@harai, this problem is tracked in #12190 (see #12190 (comment)). |
Overview
ng-if
is not used when determining the "null" option for ng-options. Consider the following select:I would expect the above example to produce a select with the null option
B
. However, ng-options actually findsA
as being the correct null option (see selectDirective). AfterA
is set as the null option,ng-if
removes it from the DOM, removing the null option as well. It would seem thatng-if
needs to be evaluated beforeng-options
. I looked through the source and was not able to find a simple fix, but I'm not familiar with angular's source either.Reproduce the error
Simple example on Plunker
Stack overflow question that lead me to this
Another, more complex, Plunker from the above stack overflow
Use Case
I have a select directive that is used in multiple places in different ways. The logic is 100% the same, the only difference is the null option:
Since the logic is the same for all three of these cases and the only difference is the null option prompt, it doesn't make sense to make 3 separate directives to handle the different cases.
Angular Version
I've verified in both 1.2.21 & 1.3.0.beta.X. I haven't looked at older versions.
Browsers and Operating System
This appears to be an AngularJS bug and not a browser issue. However, I tested and verified the bug in both Chrome 36.0.1985.125 & IE 11 on Windows 8.
The text was updated successfully, but these errors were encountered: