我试图将数据从json rest api传递到流星模板
我从这样的HTTP GET中获取JSON:
if (Meteor.is_client) {
Meteor.http.call("GET", "https://public-api.wordpress.com/rest/v1/freshly-pressed", function (err, result){
console.log(result.content);
})
}
if (Meteor.is_server) {
}
我可以在浏览器控制台中看到JSON数据
如何将数据传递给模板?
答案 0 :(得分:3)
有多种方法,具体取决于您拨打电话的位置以及您使用的套餐。最简单的方法是使用session和helper:
HTTP.get(..., function(err, result) {
Session.set('httpResult', result);
});
Template.myTemplate.json = function() {
return Session.get('httpResult');
};
<template name="myTemplate">
{{json.property}}
{{#with json}}
{{property}}
{{otherProperty}}
{{lotsOfProperties}}
{{/with}}
</template>
答案 1 :(得分:1)
最简单的方法是将结果保存在变量中,并在模板中使用#with。
Meteor.http.call("GET", "https://public-api.wordpress.com/rest/v1/freshly-pressed", function (err, result){
my_json = result.content;
})
模板:
<template name="json_data">
{{#with my_jason}}
...
{{/with}}