In AngularJS, ng-options
lets one specify an array of any values, not just strings, while preserving types. For example, I might create an HTML select
using ng-options
over an array of integers. When an option is selected, the integer value is placed into the model—no string-to-int conversion necessary.
In Elm, an option's value
attribute accepts only a string, and thus the select
's onInput
event sends back a string. Then I have to manually convert it to an int.
Does Elm have any equivalent to AngularJS's ng-options
? Or any way to use a select
with arbitrary, even non-scalar, values?
There isn't really a way to do this. You're going to have to parse from a string to your desired type and back. You could wrap a <select>
with Prism-based API.
Give me a couple hours and I'll write a tiny library for that after work... :D
EDIT:
Package - http://package.elm-lang.org/packages/toastal/select-prism/latest
Blog Post - https://toast.al/posts/2017-01-13-playing-with-prisms-for-the-not-so-isomorphic.html
You might also be interested in my elm-select
helper library. It doesn't do all that cool Prism / Lens stuff, but it might be simpler in some cases:
http://package.elm-lang.org/packages/lgastako/elm-select/1.0.0/Select
Full example code here:
https://github.com/lgastako/elm-select/blob/master/src/Main.elm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With