gpt4 book ai didi

javascript - 我的模板无法识别变量。为什么会发生这种情况?

转载 作者:行者123 更新时间:2023-11-30 17:43:21 25 4
gpt4 key购买 nike

我的 Handlebars 模板是这样的:

Template.test.people

<template name="test">
<ul>
{{#each people}}
{{ name }} //name is present in every object in the Template.test.people array.
{{/each}}
</ul>
</template>

Template.test.people 变量在用户点击按钮。届时,上述无序列表将变为未隐藏状态。然而,该列表显示为空列表!

但是,即使此时我可以去控制台读取Template.test.people变量的值,并且可以清楚地看到它有信息。该变量是一个对象数组。

我尝试了另一种解决方法,只是为了确保事情得到“更新”,并且我通过这样的函数传递了我的变量:

Template.test.helper = function(){
return Template.test.people;
}

然后将模板更改为:

<template name="test">
<ul>
{{#each helper}}
{{ name }}
{{/each}}
</ul>
</template>

这也没有用。

最佳答案

简单地重新分配静态模板数据不会触发任何类型的 react 性重新计算。将对象存储在 Session、集合或其他响应式数据源中:

Template.test.people = function() {
return Session.get( "people" );
};

Template.test.events({
"click #the-button": function() {
Session.set( "people", [{ foo: "bar" }, { baz: "qux" }] );
}
});

关于javascript - 我的模板无法识别变量。为什么会发生这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20620754/

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