I would like to set the default item in a select menu based on value rather than by index position. For example, in the sample below, if one of the radio buttons in a form displays "Oranges," and a user selects that radio button, is it possible to make the default selection in the subsequent select menu be Oranges as well? Ideally, I would prefer not to rely on the order of items in the select menu.

2 Answers
2

Not completely sure what you ui is supposed to look like or waht the data structures you are pulling from look like. But in your ng-change it looks like you are using a value that is foreign to the dataStructure you are trying to manipulate. ie. using a array value to return a nested value in an object. see if the below works for the situation. I also did a plnkr.co that shows an example of it working as well. But here are the main snippets that

This is very helpful. Thank you. If one controller is handling the radio buttons and another controller is handling the select menu, could I use that function in a service and share it between two controllers?
– KenFeb 18 '15 at 22:08

1

Yep for sure. You would need to use the $controller service to 'inject' it into the other controller. Here is a recent stackoverflow question regarding injecting another controllers scope.. Just need to follow the naming configuration pattern.
– jamieFeb 18 '15 at 22:55

thanks again. So in this case, the controller for the radio buttons will be injected into the controller for the select menu?
– KenFeb 19 '15 at 0:28

I am going to play around with it a little. When I originally looked at the issue. I thought solution should really use directives. But in answering it - i thought to stay true to your current set up would be most clear.
– jamieFeb 19 '15 at 6:16

@Ken here is my directive solution. It is a much cleaner and on its way to being reusable components. Here is more reading if you are interested in the idea of approaching things from a component approach - which is the future of angular and web dev. component based directives
– jamieFeb 22 '15 at 4:57