这是取自 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 colons
和double plus
用于连接列表?它们有何不同?
2) 在 checkbox
此行中的函数 (Signal.message address << tag)
,什么是 tag
解除绑定(bind)到?是Red
(或)red
?这个参数有什么用?
3) address
的参数类型是什么?函数取什么?
最佳答案
回答
- 双冒号
(::)
将单个元素添加到列表的开头。
双加(++)
连接两个列表。
当从 tag
将等于Red
/address
不是函数。它的类型为Address Action
,这意味着它保存可以接收Action
消息的邮箱地址。这个“邮箱”在代码中不可见,它由StartApp
内部使用,并在main
函数中使用。
checkbox 地址 model.red Red "red"< 调用 checkbox
时,
。 Red
是从 Bool
到 Action
的函数。它将复选框的事件包装在此数据构造函数中,以便稍后在 update
函数中,您可以将该复选框的事件与其他复选框的事件区分开来。
代码引用
为了使这个问题保持有用,即使链接的示例发生变化,这些是我引用的相关代码部分:
更新
函数:
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/