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

select with ng-options and track expression does not set selected element #8415

Closed
joscha opened this issue Jul 30, 2014 · 5 comments
Closed

Comments

@joscha
Copy link
Contributor

joscha commented Jul 30, 2014

Request Type: bug

How to reproduce: Create the following HTML structure:

<div ng-init="a = [{label: 'one', value: 15}, {label: 'two', value: 20}]; foo = 15">
    foo: {{foo}}<br/>
    <select ng-model="foo" ng-options="x.value as x.label for x in a track by x.value"/>
</div>

Component(s): misc core

Impact: medium

Complexity: small

This issue is related to: #8366 (probably)

Detailed Description:

Test case here: http://jsfiddle.net/joscha/6h4Md/2/
Everything works as expected except the displayed selected item is not updated.

Other Comments:

@petebacondarwin
Copy link
Contributor

@joscha this is not actually related to #8366. What you have here is an incorrect use of track by.
You only need to use track by when the model that you are binding to is an object. In your reproduction you are binding to x.value which is a number so track by is not much use.

You can see it working properly if you bind to x rather than x.value: http://jsfiddle.net/4M5Fc/

Perhaps your real-world scenario is more complex and is not solved so simply. If so can you create another issue with a more complex reproduction.

Thanks

@joscha
Copy link
Contributor Author

joscha commented Jul 30, 2014

@petebacondarwin I see, thank you - is there a short way to not write the whole object to the model then though? Because thats what I am trying to do - my object basically consists of a (unique) value and a label and your fiddle changes the model output to the whole object within my array instead of only the value part that is of interest.
x.value as x.label for x in a works that way, but messes up the value part of the <option> into the array index.

@petebacondarwin
Copy link
Contributor

That is the correct way to do it. Why do you care about the option values?

@joscha
Copy link
Contributor Author

joscha commented Jul 31, 2014

The correct value is so important for us, because we use Selenium + Watir for our integration tests and reading and setting the value of a <select> element only works if the value is the actual value, not an array index. Our current workaround is to write the model value back into a data attribute (e.g.: <select ng-model="foo" data-current-value="{{foo}}" ng-options="...">) but that is rather hacky in my eyes and also you can't use the helpers for select inputs given by Watir, so we are trying to get rid of it.

@ghost
Copy link

ghost commented Nov 14, 2017

Do you have the solution in Watir and Selenium. I have to do the same but I am not able to select the auto-complete option provided by angular js in Watir.
Regards

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants