我只想在我的模板中显示数组的第一个元素。我看过很多主题,包括 this one但它对我不起作用。
我有一个这样的 helper :
Template.home.helpers({
posts() {
return Posts.find({});
}
});
我想在我的模板中做这样的事情:
{{posts.[0].title}}
在这种情况下我不想使用 findOne。
最佳答案
最好在助手级别执行此操作,例如,这会向帖子助手添加一个可选的索引参数:
Template.home.helpers({
posts(index) {
if(typeof index !== "undefined" && index !== null){
var data = Posts.find();
return data[index];
}
else{
return Posts.find();
}
}
});
然后设置数据上下文并像这样在 blaze 中调用它:
{{#with posts 0}}
{{title}}
{{/with}}
关于javascript - meteor 在模板空格键中显示数组的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374547/