嗨,这段代码对Handlebar.js来说非常好。如何通过c#/HandleBars.net显示密钥/属性?
{{#each myObject}}
{{#if this.length}}
<b>{{@key}}</b>
{{#each this}}
<li>{{this}}</li>
{{/each}}
<br>
{{/if}}
{{/each}}发布于 2016-05-12 21:14:12
更新5/14/16
版本1.6.6 of Handlebars.Net包括两个更改:
{{length}}和{{Length}}不能解决同一问题感到惊讶) 。Length和length都存在于对象上)的情况下,绑定程序将更倾向于精确匹配)this现在可以在成员路径中使用,例如this.length原始答案
在过去的几天里,我们遇到了一些问题,这些问题涉及到、大小写敏感性、和成员决议。您是对的,这个模板应该可以正常工作;我们正在修复它。
同时,如果您将{{#if this.length}}更改为{{#if Length}},它将如您所期望的那样工作。
发布于 2016-04-25 15:27:29
var htmlTemplateStr = //Open a resource or file containing your HTML template,
// and load it into this string
var LoadedTemplate = Handlebars.Compile(htmlTemplateStr);
var contextJson = //create a JSON object that will hold all the variables your
// template will be looking for
var outputHtml = LoadedTemplate(contextJson);编辑
您的键数组将是JSON对象的JSON数组成员。要显示它,您需要向html模板中的每个块添加一个HandleBars。看起来会是这样的:
{{#each Key}}
<!-- HTML to display whatever is in each object in the Key array -->
{{/each}}https://stackoverflow.com/questions/36842497
复制相似问题