简体   繁体   English

如何从模板访问对象的属性?

[英]How Do I Access an Object's Properties From a Template?

According to http://handlebarsjs.com/expressions.html , I should be able to do this: 根据http://handlebarsjs.com/expressions.html ,我应该可以这样做:

<h1>{{article.title}}</h1>

But I can't seem to get this to work in meteor. 但我似乎无法让它在流星中发挥作用。 Here's my template: 这是我的模板:

<template name="content">
  {{#if item}}
    <p>{{item.name}}</p>
  {{/if}}
</template>

Here's the JavaScript that returns the item: 这是返回项目的JavaScript:

  Template.content.item = function() {
    return Items.findOne({ _id: Session.get("list_id") });
  };

And yes, the item does indeed have a property called name :-) 是的,该项确实有一个名为name的属性:-)

When I do this, I see an error in Firebug that says ret is undefined 当我这样做时,我在Firebug中看到一个错误,表示ret is undefined

This can be tracked down to evaluate.js: 这可以追溯到evaluate.js:

for (var i = 1; i < id.length; i++)
  // XXX error (and/or unknown key) handling
  ret = ret[id[i]];
return ret; 

At the moment of the error, ret references the window object. 在错误发生时, ret引用window对象。 What's up with that? 那是怎么回事?

You should use {{#with object}} 你应该使用{{#with object}}

If your object is something like : 如果您的对象是这样的:

my_object = {
    name : 'my_name',
    prop : 'my_prop'
}

In your template your can do : 在您的模板中,您可以:

<template name="my_template">
    {{#with my_object}}
        <p>Name is {{name}}<p>
        <p>Prop is {{prop}}</p>
    {{/with}}
</template>

Here you go :) 干得好 :)

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

相关问题 如何访问数组中对象的属性? - How do I access an object's properties in an array? 从对象数组的元素中,如何访问对象的属性/方法。 JS - From an Object's Array's Element, how can I access the Object's properties/methods. Js 如何访问位于数组内部,位于数组内部的对象的属性? - How do I access properties of an object that's inside of an array that's inside of an array that's inside of an array? 如何访问该对象的属性? - How do I access properties on this object? 在使用“this”关键字的方法中,如何从其他属性访问对象属性? - How do I access the object properties from other properties, while within a method using the 'this' keyword? 从模板到函数的访问对象属性 - Angular - Access object properties from template to function - Angular 如何从其方法内的函数访问对象的属性? - How can I access properties of object from a function that's within its methods? 如何使用 V8 从 C++ 访问和调用 Javascript 对象属性和方法? - How do I access and call Javascript Object properties and methods from C++ using V8? 如何用双引号删除Access JS对象属性 - How do I remove access JS object properties with double quotation 如何在javascript中访问for循环中的对象属性? - How do I access object properties in a for loop in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM