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

ng-value and select elements #7994

Closed
justinwinslow opened this issue Jun 26, 2014 · 13 comments
Closed

ng-value and select elements #7994

justinwinslow opened this issue Jun 26, 2014 · 13 comments

Comments

@justinwinslow
Copy link

According to the documentation (https://docs.angularjs.org/api/ng/directive/ngValue), ng-value should work with select elements but that's not the case. If you change the plunker in the example to be a select box "unicorn" does not end up selected.

@Narretz
Copy link
Contributor

Narretz commented Jun 26, 2014

input[select] doesn't even make sense, there's no such thing. Unless I'm missing something, this should simply be removed.

@Narretz Narretz added this to the Backlog milestone Jun 26, 2014
@justinwinslow
Copy link
Author

Hmm - I guess last night I was thinking ng-value on options populated by ng-repeat would look cleaner than the selected directive. I had also seen mention elsewhere that ng-value could coerce null to select an empty value. Not sure if that makes sense.

@jeffbcross
Copy link
Contributor

Technically ngValue works on hard-coded options within a select, such as:

<select>
  <option ng-value="names[0]">{{names[0]}}</option>
  //value = pizza
</select>

A PR to the docs would be good to clarify this.

@kennardconsulting
Copy link

+1

This doesn't seem to work as advertised for booleans. See this Plunker: http://plnkr.co/1Nf5xCNBYBvEyRHMvHhI

I cannot get the OPTIONS to deal in booleans. It keeps switching the type to a string?

timraymond added a commit to timraymond/angular.js that referenced this issue Jul 15, 2014
It is impossible to create an `input[select]`, so it appears the intention
here was actually `option`.

Fixes angular#7994
timraymond added a commit to timraymond/angular.js that referenced this issue Jul 15, 2014
It is impossible to create an `input[select]`, so it appears the intention
here was actually `option`.

Fixes angular#7994
@btford btford removed the gh: issue label Aug 20, 2014
@kennardconsulting
Copy link

Hi Narretz,

Can you please clarify how this relates to 'doesn't work with booleans' (see my Plunker above). Or is there now a separate issue for that?

Regards,

Richard.

@Narretz
Copy link
Contributor

Narretz commented Oct 30, 2014

@kennardconsulting
Richard, I closed this issue because it was referenced in a PR. Initially, it was about docs, so I didn't take your issue into consideration. Can you please open another issue about this?

@kennardconsulting
Copy link

@Narretz yes, I would be happy to. Before I do, however, could you please clarify my question (as it is related to the documentation you just modified). The question is: when you use ng-value on an option, is it expected that the expression always gets converted to a string? So...

$scope.myBooleanScopeValue = true;
...
<option ng-value="myBooleanScopeValue">

...will always return "true" never true? This causes problems, as can be seen in the Plunker.

@Narretz
Copy link
Contributor

Narretz commented Oct 31, 2014

@kennardconsulting
It is indeed expected that ng-value can only deal with string values. That is because it $watches the expression inside ng-value and sets the result to the value of option, which is then picked up by the select directive to populate the model. And since html attributes can only contain strings, ng-value can only be used for strings.

@Narretz
Copy link
Contributor

Narretz commented Oct 31, 2014

Note that the documation for select has recently been updated with this info: https://code.angularjs.org/snapshot/docs/api/ng/directive/select

@Narretz
Copy link
Contributor

Narretz commented Oct 31, 2014

Correction: only using option together with ng-value does not support values other than strings. Angular actually supports this for input[radio] via the attributes getters / setters.

@kennardconsulting
Copy link

@Narretz thanks for investigating this for me. I think you have identified the source of confusion: some parts of Angular support values other than strings, others not.

'Pure' html does not support boolean radio buttons, but Angular does (as you said). Is there not an approach that will allow a select box of booleans?

@Narretz
Copy link
Contributor

Narretz commented Nov 2, 2014

You can use ngOptions: https://docs.angularjs.org/api/ng/directive/select

@kennardconsulting
Copy link

Much appreciated. Thanks.

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

No branches or pull requests

5 participants