【问题标题】:Cannot call method 'match' of undefined handlebar issue无法调用未定义车把问题的方法“匹配”
【发布时间】:2014-01-23 19:48:34
【问题描述】:

我的模板带有把手,并且工作时间最长,但有时会给我这个错误无法调用未定义的方法“匹配”。这是我的代码

查看

      <div id="template"></div>
            <script id="template-script" type="text/x-handlebars-template">  
                {{#measures_list}}      
                    <div class="col-lg-7  col-lg-offset-1">                     
                            <p class="indicator-title gray-bottom-border">{{name}}</br></p>
                            <div>
                                <p><span class="font30px">{{total}} </span> Total {{name}}<br><span class="green-text">{{increase}}%</span> from last <span class="metric_time"></span></p>
                            </div>
                            <div class="graph-header">
                                <h3>{{this_interval}} </h3><p>New {{name}}</p>
                                <p class="percentage-graph green-text">{{percentage}}%<p>
                            </div>                      
                        <div id={{graph_id}} class="graph"></div>

                    </div>
                {{/measures_list}} 
            </script>

js

   var source=$("#template-script").html();
   var template=Handlebars.compile(source);
   $('#template').empty().append(template(template_data));
   console.log('source'+source+'template'+template)

我使用console.log查看错误,所以很多次都可以正常工作但在某些情况下源未定义,页面出现多次,例如我单击一个按钮并在我关闭页面并单击后出现再次按下按钮并多次执行此操作,多次或第一次之后给我错误:无法调用未定义的方法“匹配”

请帮忙

【问题讨论】:

    标签: javascript jquery templates handlebars.js


    【解决方案1】:
    var source=$("#template-script").html() || '';
    

    【讨论】:

    • if (!source) console.log("嘿!你在这里传递了一个空变量!!")
    【解决方案2】:

    我将所有脚本标签之间的所有内容都放在我的 js 中,就像一个变量并调用而不是源代码,我的修复是因为有时无法找到 sourcesource 在那里,所以我更改为我的 js 并且正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多