简体   繁体   English

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

[英]My template doesn't recognize a variable. Why does this occur?

My handlebars template has something like this: 我的车把模板具有以下内容:

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>

The Template.test.people variable is instantiated ( Template.test.people = [...] ) at some point during runtime when the user clicks on a button. 当用户单击按钮时,在运行时的某个时候实例化Template.test.people变量( Template.test.people = [...] )。 At which time, the above unordered list becomes unhidden. 这时,上面的无序列表将被隐藏。 The list however displays as an empty list! 但是,该列表显示为空列表!

However, even though I can go to the console and read the value of the Template.test.people variable, at this time, and can clearly see that it has information. 但是,即使此时我可以转到控制台并读取Template.test.people变量的值,也可以清楚地看到它具有信息。 The variable is an array of objects. 变量是对象数组。

I tried another workaround just to make sure that things were getting "updated", and I passed my variable through a function like so: 我尝试了另一种变通办法,只是为了确保事情得到“更新”,然后通过这样的函数传递变量:

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

and then change the template to this: 然后将模板更改为此:

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

which also did not work. 这也没有用。

Simply reassigning static template data does not trigger any kind of reactive recalculation. 简单地重新分配静态模板数据不会触发任何形式的反应式重新计算。 Store the objects in Session , a collection, or another reactive data source: 将对象存储在Session ,集合或其他反应性数据源中:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么sinon不认得我的存根? - Why doesn't sinon recognize my stub? 尝试通过将状态对象放入变量来压缩我的ui路由器状态定义。 不起作用 - Trying to compress my ui-router state definitions by putting the state object into a variable. Doesn't work 将构造函数赋值给变量。 - 为什么这段代码不能使用firefox控制台 - assignning constructor function to a variable. - why doesn't this code work, using firefox console 为什么内部html的src属性不能识别我动态生成的变量? - Why doesn't the src attribute of inner html recognize my dynamically-generated variable? HTML表单和全局变量:为什么不能识别全局变量? - HTML Form and global variables: Why doesn't it recognize the global variable? VueJS 无法识别我在花括号之间的变量 - VueJS doesn't recognize my variable between curly braces 未捕获ReferenceError:未定义loadData! 为什么无法识别我的功能? - Uncaught ReferenceError: loadData is not defined! Why doesn't it recognize my function? 将函数返回变量的函数。 为什么需要额外的()集? - Function returning a function to a variable. Why does it need the extra set of ()? 无法识别数据变量。 未捕获的ReferenceError:未定义edu_id - Can't recognize data variable. Uncaught ReferenceError: edu_id is not defined 树枝模板无法识别Javascript - Twig Template doesn't recognize Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM