gpt4 book ai didi

javascript - 使用模板事件触发另一个模板中的响应

转载 作者:行者123 更新时间:2023-11-29 19:59:10 24 4
gpt4 key购买 nike

当一个事件在一个模板中发生时,我想在另一个模板中做一些事情。

这是我的代码:

客户端 html:

    <body>
Navigation:
{{> navigation}}
=======================================================
Body:
{{> body}}
</body>


<template name="navigation">
<input type="button" value="MenuBtn" />
</template>
<template name="body">
{{content}}
</template>

JavaScript:

    Template.navigation.events({        'click' : function (e) {                //Nothing happened in {{body}}            Template.body.content = function(){             // i want to do something... like query database             // maybe, just a example : return peple.find({"name":e.currenTarget.value});                                 return "hello";                   };                  } });

知道我做错了什么吗?

最佳答案

使用 handlebars 将数据传递给模板,以便模板系统知道何时重绘 html

客户端JS

Template.body.content = function() {
return Session.get("content") || "Hi there, click the button"
};

Template.navigation.events({
'click' : function () {
alert("1");// it's normal
Session.set("content", "<h1>HELLO!!!! '+ people.findOne({"name":e.currenTarget.value}).name || " unregisterd person" + '</h1>");
alert("2");//it's normal
}
});

我还假设你要做一些 HTMLey 并且你不希望它被转义所以确保你使用:

<template name="body">
{{{content}}}
</template>

关于javascript - 使用模板事件触发另一个模板中的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com