elm - 参数,Html 复选框接受 elm

标签 elm

这是取自 elm 复选框 examples 的 View 片段-

view address model =
  div [] <|
    span [toStyle model] [text "Hello, how are yo?"]
    :: br [] []
    :: checkbox address model.red Red "red"
    ++ checkbox address model.underline Underline "underline"
    ++ checkbox address model.bold Bold "bold"

checkbox : Address Action -> Bool -> (Bool -> Action) -> String -> List Html
checkbox address isChecked tag name =
  [ input
      [ type' "checkbox"
      , checked isChecked
      , on "change" targetChecked (Signal.message address << tag)
      ]
      []
  , text name
  , br [] []
  ]

1) 我明白,double colonsdouble plus用于连接列表?它们有何不同?

2) 在 checkbox此行中的函数 (Signal.message address << tag) ,什么是 tag解除绑定(bind)到?是Red (或)red ?这个参数有什么用?

3) address 的参数类型是什么?函数取什么?

最佳答案

回答

  1. 双冒号 (::)将单个元素添加到列表的开头。
    双加(++)连接两个列表。
  2. 当从 checkbox 地址 model.red Red "red"< 调用 checkbox 时,
  3. tag 将等于 Red/Red 是从 BoolAction 的函数。它将复选框的事件包装在此数据构造函数中,以便稍后在 update 函数中,您可以将该复选框的事件与其他复选框的事件区分开来。
  4. address 不是函数。它的类型为Address Action,这意味着它保存可以接收Action 消息的邮箱地址。这个“邮箱”在代码中不可见,它由 StartApp 内部使用,并在 main 函数中使用。

代码引用

为了使这个问题保持有用,即使链接的示例发生变化,这些是我引用的相关代码部分:

更新函数:

update action model =
  case action of
    Red bool ->
        { model | red <- bool }

    Underline bool ->
        { model | underline <- bool }

    Bold bool ->
        { model | bold <- bool }

操作类型:

type Action
  = Red Bool
  | Underline Bool
  | Bold Bool

main 函数:

main =
  StartApp.start { model = initialModel, view = view, update = update }

关于elm - 参数,Html 复选框接受 elm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31680217/

相关文章:

module - 如何导入本地模块?

elm - 如何在 Elm 中 Debug.log 信号?

elm - 对于完整的应用程序/站点,Elm “stack”是什么样的?

record - 如何在 Elm 模型中添加嵌套元素

elm - 从子组件更改全局状态

functional-programming - 在 Elm 中,如何迭代 map ?

pattern-matching - 为什么从 Elm 中移除了案件 guard ?

arguments - 如何减少 elm 中的争论?

svg - Elm:向 SVG 元素添加点击事件不起作用——这可能吗?

go - 当参数与工作示例相同时,为什么 Elm 不会为 JSON 数据示例编译这个 HTTP 请求?