【问题标题】:Template not working with JSON / Mustache.js模板不适用于 JSON / Mustache.js
【发布时间】:2013-01-07 18:10:01
【问题描述】:

我目前遇到了 Mustache.js 模板的问题。一切似乎都是正确的,并且模板在页面上加载为 HTML。但它无法访问 JSON 数据,我不太清楚为什么。任何人都可以帮忙吗?提前致谢。

我使用的 Javascript 如下。

    query.find({
        success: function(results){
        var template = $("#newCurrItem").html();


        var newContents = Mustache.to_html(template, results);
        $("#curr-list").append(newContents);
        },
        error: function(error){
            console.log("error");
        } 

    });

此图显示 JSON 格式:

http://i.imgur.com/JrYrORk.png?1

还有,这里是模板:

<script id="newCurrItem" type="text/html"> 
    {{#results}}
    <!-- Template for new curriculum --> 
    <li id="curr-list-item">


            <div id="curr-item">
            <input type="checkbox" class="item-delete">
            <label id="item-content" class="item-content">{{curr}}</label>

    </div>
    </li>
    {{/results}}
</script>

【问题讨论】:

标签: javascript jquery jquery-templates mustache


【解决方案1】:

尝试像这样构建您的 JSON 数据:

    { "results" : [
        {
            "curr": "curr_dbc",
            "createdAt" : ...,
            "updatedAt":...
         }
     ]};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2015-07-23
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多