-
Notifications
You must be signed in to change notification settings - Fork 27.4k
ng-options track by and select as are not compatible #6564
Comments
Oops, accidentally deleted my previous comment. |
+1 I'm having the same problem. |
It's annoying but you can really just leave away the |
Can confirm this problem still exists as of v1.3.0-beta.15. |
Having the same problem with 1.2.20 :/ |
Same here |
Ok, I have investigated this. It's not actually a bug, just misleading documentation. 1. You want a shiny presentation for an ugly valueUse
When you change the dropdown, the model is assigned whatever you define in front of the 2. You want to iterate over complex objectsUse
When you change the dropdown, the model is assigned the item. The Illustrated in http://jsfiddle.net/0vpsv1wa/1/ |
ngOptions introduced `track by` in c32a859. Using `track by` puts constraints on the value you can use in the interpolation expression in ngOptions. This patch both documents this and adds an exception if you use ngOptions in an unsupported way. Closes angular#6564
ngOptions introduced `track by` in c32a859. Using `track by` puts constraints on the value you can use in the interpolation expression in ngOptions. This patch both documents this and adds an exception if you use ngOptions in an unsupported way. Closes angular#6564
Faced the same problem, and figured a workaround, but I don't know if this is going to solve future issues (at least in the project I'm doing this). Somewhere on your code add this ugly script. $(document).on('change', 'select', function(e){
var scope = angular.element($(this)).scope();
var val = scope[$(this).attr('name')];
$(this).val(val);
}); |
ngOptions introduced `track by` in c32a859. Using `track by` puts constraints on the value you can use in the interpolation expression in ngOptions. This patch documents this. Closes angular#6564
Interestingly, if I remove 'ng-options': 'item.id as item.name for item in values' Expected '0' to equal '10 |
@jeffbcross: This is "expected", since with The test should rather be:
(And removing |
Hi @tbosch, I have constructed a Plunkr example that illustrates the main place I was relying on both Essentially, I have an array of options that contains properties that are used for the I would then have a So the example you cited could be fixed to use this technique to have: Is this not a valid use case? |
This commit implements two functions, "isSelected()" and "getViewValue()" to properly compute an option's selected state and the model controller's viewValue respectively. These functions give proper precedence to "track by" and "select as" parts of the ngOptions comprehension expression, which were previously inconsistent and incompatible. Fixes angular#6564
@greglockwood I forked the plnkr and changed the expression to work with rc.5. I just changed <!--Before-->
<select ng-model="data.value" ng-options="opt.value as opt.display group by opt.groupName for opt in options track by (opt.id || opt.value.id)"></select>
<!--After-->
<select ng-model="data" ng-options="opt.display group by opt.groupName for opt in options track by (opt.id || opt.value.id)"></select> Your |
Reopening so we can give this some more consideration. After some discussion with @tbosch and @IgorMinar, here are some proposed approaches to allowing
|
Instead of throwing an error when using "track by" and "select as" expressions, ngOptions will assume that the track by expression is valid, and will use it to compare values. Closes angular#6564
Changing the milestone since |
We ended up deciding to just make the old behavior possible. Working on a PR |
@jeffbcross This is a tricky one. Whilst I am grateful for undoing the change to make the old behaviour possible, I agree it was confusing. In particular, that you have to construct an expression for the In response to your fork of my plnkr, I offer you a counter-fork which demonstrates something closer to my actual use case, in that it is indeed using a "shared model". In such a case, it is not valid to overwrite the parent object with the complete option selected, as the As for your proposed solutions, I don't think either of them is the perfect solution, as they both have their downsides. The first is probably the better of the two, though. Whilst it is a breaking change, it is not a huge deal to cope with, as most people would just find themselves deleting My main misgiving about this change is that the context for the Here is a graphic to illustrate what I mean: As you can see, there would be 4 different contexts within a single ng-options expression with this change:
In particular, that the The cons of the second solution, introducing a
The biggest thing in its favour is that it is an explicit context, which is potentially less confusing for people and it is easier to document and explain it as a special "double purpose" context. It would also be fairly easy to migrate to. Anyways, those are just my thoughts (with input from a talented co-worker). |
Instead of throwing an error when using "track by" and "select as" expressions, ngOptions will assume that the track by expression is valid, and will use it to compare values. Closes angular#6564
Thanks for bringing the issue up, and thanks for the feedback on the approaches. This is a tough problem without a clean solution that satisfies all constraints, is consistent with ng-repeat, and is backwards-compatible. In the meantime, I removed the thrown error, added tests for how track by and select as should work together, and fixed the implementation. Unfortunately, the originally reported issue would not work with the expression/model as is, but could work with some re-working. |
This is still happening in v1.4.7, no fix yet? |
same problem here!! |
Same problem!! |
same thing here with in v1.4.7 |
Same thing with v1.4.8 |
Same thing with v1.5.3 |
The ng-options works fine, but when update the model manual, the select value hadn't update.
|
There are certain (documented) limitations in using |
ng-options="item.id as item.name for item in values track by item.id" works, but doesn't se the default value |
No, it doesn't work (i.e. track by has no effect). |
is there a solution to this yet? |
When both a track by expression and a select as expression are used in ng-options, binding doesn't work anymore.
For instance:
It seems that in the ngOptions directive, the track function always directly use the value expression even if a select as expression is set.
The text was updated successfully, but these errors were encountered: