[英]Meteor | Helper function. How to print attribute values of returned element on HTML
I am using Meteor JS for UI development of a project.我正在使用 Meteor JS 进行项目的 UI 开发。 There is a helper method which takes a input parameter and return an object.
有一个辅助方法,它接受一个输入参数并返回一个 object。
Helper Function:助手 Function:
"getPhoneName": function (param) {
let myObj = PhoneRegister.findOne({"_id": param});
//This myObj contains name, _id, modelNumber
return myObj;
}
How i am using it on HTML我如何在 HTML 上使用它
<span>{{getPhoneName 'id_1234'}}</span> //This line is obviously prints the [[Object]]
Here i am not getting solution how to print modelNumber. I have tried following :
1.{{getPhoneName 'id_1234'.modelNumber}}
2.{{getPhoneName 'id_1234'}}.modelNumber
Can someone help?有人可以帮忙吗? If you need more information please let me know.
如果您需要更多信息,请告诉我。
You have mentioned you have this helper function:你提到你有这个助手 function:
getPhoneName (_id) {
const myObj = PhoneRegister.findOne({_id});
//This myObj contains name, _id, modelNumber
return myObj;
}
There are 2 ways to go based on your needs根据您的需要,go 有 2 种方法
First, if you want to get just the "name" as the helper implies getPhoneName
, you can instead return myObj.name;
首先,如果您只想获取助手所暗示的“名称”
getPhoneName
,则可以改为return myObj.name;
(error handling to ensure myObj exists should also be considered). (还应考虑确保 myObj 存在的错误处理)。
You can then just use it:然后你可以使用它:
<p>The name of the phone is {{getPhoneName phoneId}}</p>
But, if you instead what to list a variety of information about the phone in your template, you can leave the helper alone and adjust your template in one of two different ways:但是,如果您改为在模板中列出有关手机的各种信息,您可以不理会帮助程序,并通过以下两种不同方式之一调整您的模板:
1) Use #let
to name your object within your template. 1) 使用
#let
在您的模板中命名您的 object。
{{#let currentPhone=(getPhoneName phoneId)}}
<ul>
<li>ID: {{currentPhone._id}}</li>
<li>Name: {{currentPhone.name}}</li>
</ul>
{{/let}}
2) Use #with
to use that object as your data context without assigning a name 2) 使用
#with
将该 object 用作您的数据上下文而不分配名称
{{#with getPhoneName phoneId}}
<ul>
<li>ID: {{_id}}</li>
<li>Name: {{name}}</li>
</ul>
{{/let}}
If you only want one property, you could simply return myObj.property;
如果你只想要一个属性,你可以简单地
return myObj.property;
and display it as you already did with {{getPhoneName 'id_1234'}}
.并像使用
{{getPhoneName 'id_1234'}}
一样显示它。
But if you want to access multiple properties, you can return the object in an array and then use each or each-in Blaze's built-in block like this:但是如果你想访问多个属性,你可以在一个数组中返回 object,然后使用每个或每个Blaze 的内置块,如下所示:
Helper:帮手:
"getPhoneName": function (param) {
let myObj = PhoneRegister.findOne({"_id": param});
//This myObj contains name, _id, modelNumber
return [myObj]; // Returning an array in order to use each or each-in
}
HTML (with each): HTML(每个):
{{#each getPhoneName 'id_1234'}}
<span>{{this.property1}}</span>
<span>{{this.property2}}</span>
{{/each}
You can also take a look at {{#let}} , in some cases it's useful too.你也可以看看{{#let}} ,在某些情况下它也很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.