2016-06-11 43 views
3

模式,我有一個這樣的輸入:榆樹如何更新基於輸入型號

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

如何更新模型?我有這樣的事情:

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

我不知道,如果在輸入類型號的值是一個IntString。 我也試試這個:

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 
+0

你的第二次嘗試看起來不錯。什麼不工作? – farmio

+0

我添加了錯誤 – BoumTAC

回答

9

改變函數名nb設置爲別的,因爲它已經被指定爲一個字符串,你不能覆蓋它。

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