Jquery 将未定义的传递给车把。编译

Jquery 将未定义的传递给车把。编译,jquery,handlebars.js,Jquery,Handlebars.js,我刚开始使用handlebar.js,遇到了一个小问题 我使用的是通过jQuery从JSON获得的1.0.0版宽度数据。一切正常,我的数据被拉到模板中,数据按预期显示,但我一直遇到这个错误 Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed undefined 当我在控制台中记录源代码时,我知道这不是一个字符串,但这就是本教程所解释的。我错了吗 {{#sets}} <li cl

我刚开始使用handlebar.js,遇到了一个小问题

我使用的是通过jQuery从JSON获得的1.0.0版宽度数据。一切正常,我的数据被拉到模板中,数据按预期显示,但我一直遇到这个错误

Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed undefined
当我在控制台中记录源代码时,我知道这不是一个字符串,但这就是本教程所解释的。我错了吗

{{#sets}}
<li class="products">
    <h1>{{title}}</h1>
    <img src="{{img}}">
</li>
{{/sets}}
JSON

}

希望我这里有足够的信息,谢谢你的帮助。

我想只要去掉{{{{set}}{{/set}}就行了。您知道jquery已经将对象分解为每个集合。所以你只需要

<script id="full-sets-template-mobile" type="text/x-handlebars-template">
    <li class="products">
        <h1>{{title}}</h1>
        <img src="{{img}}">
    </li>
</script>

  • {{title}}

  • 在您的模板中

    这将如何影响
    Handlebar.compile(未定义)
    问题?所有内容都由#集合封装,如果Handlebar看不到集合,则无法编译其余内容,但Handlebar将永远看不到,因为它Handlebar根本看不到模板文本<当调用
    handlebar.compile(source)
    时,code>source是
    未定义的
    ,这就是错误消息的意思。模板本身可能会出现问题。您还会注意到,模板函数是用完整的
    json
    调用的,而不是
    $的片段。每个(json.set,…)
    都将生成。至于上面的mu注释太短。他是对的,你可以使用$('#dataorbit).append(html);虽然
    function getProductsSets() {
      $.getJSON('products/products.json', {
          format: "json"
        }).done(function(json) {
          $.each(json.sets, function() {
            var source = $('#full-sets-template-mobile').html();
            console.log(source)
            var template = Handlebars.compile(source);
            var data = template(json);
            var html = $('#sets-template-inner').html(data);
          });
        }).fail(function() {
          console.log('failed');
        });
    }
    
    {
        "sets":
        [
                {
                        "title": "raw bones",
                        "img": "img/sets/set1.jpg",
                        "desc": "Raw pine table with 2 chairs and a bench.",
                        "base": 1200,
                        "seating":
                        [
                                {
                                        "price": 0,
                                        "name": "4 seater"
                                },
                                {
                                        "price": 400,
                                        "name": "6 seater"
                                },
                                {
                                        "price": 800,
                                        "name": "8 seater"
                                }
                        ]
                },
                {
                        "title": "sky blue",
                        "img": "img/sets/set1.jpg",
                        "desc": "Raw pine table with 2 chairs and a bench",
                        "base": 1300,
                        "seating":
                        [
                                {
                                        "price": 0,
                                        "name": "4 seater"
                                },
                                {
                                        "price": 500,
                                        "name": "6 seater"
                                },
                                {
                                        "price": 800,
                                        "name": "8 seater"
                                }
                        ]   
                }
        ]                
    
    <script id="full-sets-template-mobile" type="text/x-handlebars-template">
        <li class="products">
            <h1>{{title}}</h1>
            <img src="{{img}}">
        </li>
    </script>