2013-06-13 57 views
0

我需要從json中獲取內容並將其呈現給我的模板。 我嘗試使用內聯json格式,其工作正常。 請幫我加載位於其他目錄或其他服務器的外部json文件。如何在handlebarjs中加載json文件

聯腳本

var src = document.getElementById("semantic-template").innerHTML; 
    var tmpl = Handlebars.compile(src); 
    var json = {"leagueName":"American League", "abbr":"AL", "teams":[ 
      {"teamName":"Tampa Bay", "w":96, "l":66}, 
      {"teamName":"NY Yankees", "w":95, "l":67}}; 
    document.getElementById("content-template").innerHTML = tmpl(json); 

HTML

<div id="content-template"></div> 
    <script id="semantic-template" type="text/x-handlebars-template"> 
    <caption>{{leagueName}}</caption> 
    </script> 

我的外部JSON格式

[{ 
    "pics" : "images/item-1.png", 
    "label" : "Careers", 
    "text" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit." 
}] 

請提供此

+0

爲了什麼 {{標題}}是用於? –

+0

感謝您指出輸入錯誤 –

+0

您是否找到了解決此問題的方案? – Notflip

回答

0

與jQuery方法的getJSON和數據試過建議也越來越顯示在我的意見

$.getJSON("assests/json/carousel-data.json", function(data) { 
    $("#content-template").html(tmpl({objects:data})); 
});