我使用underscore.js模板庫和我的主幹示例。我的模板看起來是這樣的:如何使用underscore.js輸出JSON對象?
<script id="results-template" type="text/template">
<h2><%= title %></h2>
</script>
JSON對象看起來是這樣的:
{"src":"placeholder.jpg","title":"an image placeholder","coordinates":[0,0],"tags":["untagged"],"location":"home"}
我想通過我的模板,但我通過我的控制檯得到錯誤解析這個對象是:
Uncaught ReferenceError: title is not defined
我在做什麼錯?現場小提琴是在這裏:http://jsfiddle.net/amit_e/muLjV/46/
dahh !!我困惑了兩個JSON函數。所以你說'myPhoto.toJSON()'將對象傳遞給模板,但'stringify'函數不會? – 2012-01-04 09:25:09
我在說,myPhoto.toJSON()返回一個json對象,而JSON.stringify返回一個字符串。檢查這個jsfiddle看看有什麼不同。 http://jsfiddle.net/saelfaer/muLjV/55/(請記住檢查控制檯,它在結果窗口中沒有輸出) – Sander 2012-01-04 10:05:12
明白了!感謝例子@Sander – 2012-01-04 10:10:07