如何在氮气中调用erlang函数?

如何在氮气中调用erlang函数?,erlang,nitrogen,Erlang,Nitrogen,如何通过单击按钮调用imafunction(Param1,Param2)函数及其参数?您需要通过回发来完成此操作 最简单的方法是更改按钮以包含postback属性: -module (blah). -compile(export_all). -include_lib("nitrogen_core/include/wf.hrl"). main() -> #template { file="./site/templates/bare.html" }. title() -> "Welc

如何通过单击按钮调用imafunction(Param1,Param2)函数及其参数?

您需要通过回发来完成此操作

最简单的方法是更改按钮以包含
postback
属性:

-module (blah).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").

main() -> #template { file="./site/templates/bare.html" }.

title() -> "Welcome to Nitrogen".

body() ->
#button { id=calcButton, text="Click"}.

imafunction(Param1, Param2) -> %something here%.
然后,您必须使用
事件/1
函数处理回发:

#button { id=calcButton, text="Click", postback=do_click}.
但是,如果您想将值与某种动态数据一起传递,可以采用以下两种方法之一

1)您可以将其作为回发的一部分传递,并在回发值上进行模式匹配

event(do_click) -> 
    imafunction("first val","second val").
然后在回发上进行模式匹配

#button { id=calcButton, text="Click", postback={do_something,1,2} }
或,2)您可以将值作为输入传递(例如文本框或下拉框)

首先,添加要发送的参数字段,并确保按钮执行回发

%% Notice how this is matching the tuple in the postback
event({do_something,Param1,Param2}) ->
    imafunction(Param1,Param2).
然后在
事件/1
函数中,我们将检索值并调用函数

body() ->
    [
        #label{text="Param 1"},
        #textbox{id=param1},
        #br{},
        #label{text="Param 2"},
        #textbox{id=param2},
        #br{},
        #button{ id=calcButton, text="Click", postback=do_other_thing}
   ].
您可以在以下位置阅读更多关于氮回发和提交数据的信息:

event(do_other_thing) ->
    Param1 = wf:q(param1),
    Param2 = wf:q(param2),
    imafunction(Param1,Param2).