我使用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