fad*_*ril 11 javascript backbone.js underscore.js underscore.js-templating
我有这个示例代码使用下划线模板来呈现简单的unescapedHTML.
var template = $(this.el).html(_.template(this.template, {'data': '<script>'}));
$(this.parent).append(template);
Run Code Online (Sandbox Code Playgroud)
但是当它尝试渲染它时,它会导致错误:
未捕获的TypeError:对象[object Object]没有方法'replace'
任何人都可以请教我是什么原因以及如何解决它?由于在下划线文档中:
var template = _.template("<b><%- value %></b>");
template({value : '<script>'});
=> "<b><script></b>"
Run Code Online (Sandbox Code Playgroud)
提前致谢.
mu *_*ort 27
从精细手册:
模板
_.template(templateString, [context])将JavaScript模板编译为可以评估渲染的函数.
第一个参数_.template应该是一个字符串,而不是一个jQuery对象._.template调用String#replace函数的内部处理的一部分,也就是错误的来源.你可能想要使用它:
var template = $(this.el).html(_.template(this.template.html(), {'data': '<script>'}));
$(this.parent).append(template);
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/ambiguous/wPu6G/
你给出的例子很好:
因此,我不知道您的评论中提到的"价值"未定义的错误可能来自.
| 归档时间: |
|
| 查看次数: |
26096 次 |
| 最近记录: |