2015-08-25 27 views
0

嗨,我有以下问题。我试图循环每一章输出一节经文,但没有任何显示。循环与handlebar.js

<div class="content-placeholder"></div> 

<script id="built-in-helpers-template" type="text/x-handlebars-template"> 
    {{#each chapter}} 
    <ol> 
     <li> {{ verse}}</li> 
    </ol> 
    {{/each}} 
</script> 

<script type="text/javascript"> 

$(function() { 

    // Grab the template script 
    var theTemplateScript = $("#built-in-helpers-template").html(); 

    // Compile the template 
    var theTemplate = Handlebars.compile(theTemplateScript); 

    // We will call this template on an array of objects 
    var content = ""; 
    jQuery.ajax({ 
    url:'https://getbible.net/json', 
    dataType: 'jsonp', 
    data: 'p=John1&v=kjv', 
    jsonp: 'getbible', 
    success:function(json){ 
     content = json; 
     console.log(content); 
    }, 
    }); 

    // Pass our data to the template 
    var theCompiledHtml = theTemplate(content); 

    // Add the compiled html to the page 
    $('.content-placeholder').html(theCompiledHtml); 

}); 

</script> 

的jsfiddle(https://jsfiddle.net/gdiamond/v8dnn35j/1/

+0

谢谢@torazaburo!您可以发布答案,以便我可以对其进行投票。 :) –

回答

1

你呈现与未填充但由于Ajax调用尚未完成内容的模板。将theCompiledHtml计算并将结果分配到content-placeholdersuccess回调中。