elm如何根据输入类型编号更新模型

时间:2016-06-11 23:46:40

标签: input numbers int elm

我有这样的输入:

input [ type' "number", onInput NewValue ] [ text <| toString model.value ]

如何更新模型?我有这样的事情:

NewValue nb ->
      ( { model | value = nb }, Cmd.none )

我不知道输入类型编号中的值是Int还是String。 我也试试这个:

NewValue nb ->
  let
    nb = Result.withDefault 0 (String.toInt nb)
  in
    ( { model | value = nb }, Cmd.none )

第二个版本我收到了这个错误:

The return type of function `withDefault` is being used in unexpected ways.

44|         nb = Result.withDefault 0 (String.toInt nb)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The function results in this type of value:

    Int

Which is fine, but the surrounding context wants it to be:

    String

1 个答案:

答案 0 :(得分:10)

将函数名称nb更改为其他名称,因为它已被指定为字符串,您无法覆盖它。

NewValue nb ->
  let
    newInt = Result.withDefault 0 (String.toInt nb)
  in
    ( { model | value = newInt }, Cmd.none )