大佬教程收集整理的这篇文章主要介绍了扩展Elm教程表单应用程序以包含编号输入Age,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
那里的文字对我有意义,我的问题与它在页面底部列出的练习有关.它问我:
我对此有困难,因为onInput函数似乎只接受String输入.我觉得奇怪的是没有等效的type =“number”输入.
然而,这是我的尝试不起作用:
import Html exposing (..) import Html.App as Html import Html.Attributes exposing (..) import Html.Events exposing (onInput) import String exposing (length) main = Html.beginnerProgram { model = model,view = view,update = update } -- MODEL type alias Model = { name : String,password : String,passwordAgain : String,age : Int } model : Model model = Model "" "" "" 0 -- updatE type Msg = Name String | password String | passwordAgain String | AgE int update : Msg -> Model -> Model update msg model = case msg of Name name -> { model | name = name } password password -> { model | password = password } passwordAgain password -> { model | passwordAgain = password } Age age -> { model | age = age } -- VIEW view : Model -> Html Msg view model = div [] [ input [ type' "text",placeholder "Name",onInput Name ] [],input [ type' "password",placeholder "password",onInput password ] [],placeholder "Re-enter password",onInput passwordAgain ] [],input [ type' "number",placeholder "Age",onInput Age ] [],viewValidation model ] viewValidation : Model -> Html msg viewValidation model = let (color,messagE) = if model.password /= model.passwordAgain then ("red","passwords do not match!") else if length model.password <= 8 then ("red","password must be more than 8 characters!") else ("green","OK") in div [ style [("color",color)] ] [ text message ]
我得到的错误如下:
-- TYPE MIsmaTCH ----------------------------------------------------- forms.elm The argument to function `onInput` is causing a mismatch. 58| onInput Age ^^^ Function `onInput` is expecTing the argument to be: String -> a But it is: Int -> Msg
注意:我知道我可以创建Age输入作为另一个文本输入,但练习特别要求我检查它是一个`数字类型.我认为这意味着我应该将它作为Int保存在模型中.
我很清楚错误是什么.我只是想知道在Elm中解决这个问题的惯用方法.谢谢.
您的模型期望它是一个Int
使用String.toInt从字符串值中解析整数值.
调整更新功能以将类型转换为Int并将类型签名更改为Age String
Age age -> case String.toint age of Ok val -> { model | age = val } -- Notify the user,or simply ignore the value Err err -> model
这样您就可以选择通知用户有关错误的信息.
如果Maybe值更适合你,整个语句可以简化为:
Age age -> { model | age = Result.toMaybe (String.toint agE) }
以上是大佬教程为你收集整理的扩展Elm教程表单应用程序以包含编号输入Age全部内容,希望文章能够帮你解决扩展Elm教程表单应用程序以包含编号输入Age所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。