【问题标题】:elm how to update model based on input type number榆树如何根据输入类型号更新模型
【发布时间】:2016-10-12 16:12:05
【问题描述】:

我有这样的输入:

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

【问题讨论】:

  • 您的第二次尝试对我来说看起来不错。什么不工作?
  • 我添加了错误

标签: input numbers int elm


【解决方案1】:

将函数名称 nb 更改为其他名称,因为它已经分配为字符串并且您无法覆盖它。

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

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 2016-02-05
    • 1970-01-01
    • 2018-05-30
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多