Javascript 主干呈现函数:意外标记非法

Javascript 主干呈现函数:意外标记非法,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,尝试呈现backbone.js模板时出现以下错误: Uncaught SyntaxError: Unexpected token ILLEGAL 从以下代码第2行调用html: render: function() { $(this.el).html(_.template(contactTemplate, { model: this.model.toJSON(), })); return this; } 我不明白

尝试呈现backbone.js模板时出现以下错误:

Uncaught SyntaxError: Unexpected token ILLEGAL
从以下代码第2行调用html:

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }
我不明白这个非法角色是什么,或者正在发生什么,任何帮助都将不胜感激

编辑: 感谢您的帮助,您是正确的,我的模板有问题,结果我有:

<p><a href="#profile/<%=model.accountId%">View</a></p>

而不是

<p><a href="#profile/<%=model.accountId%>">View</a></p>


编码的乐趣:)

当您试图从模型中访问未定义的字段时,会出现该错误。通过查看代码,在尝试获取json值时,模板必须如下所示:

<b> the value of field AAA is <%= model.AAA %> </b>
然后你可以做类似的事情

<b> the value of field AAA is <%= AAA %> </b>
字段AAA的值为

我想主干网一定让你有点疯狂了

render: function() {
        $(this.el).html(_.template(contactTemplate, {
            model: this.model.toJSON(),
        }));
        return this;
    }
只有在模板中指定了
这样的字段时,才有效(我认为)。试试这个:

render: function() {
        $(this.el).html(_.template(contactTemplate, this.model.toJSON()));
        return this;
    }

您是否通过
\.template(contactTemplate,{model:this.model.toJSON()})获得任何结果。然后尝试删除
toJSON
调用后的额外
。如果您发布模板,可能会有所帮助,因为您现在得到了两个基本相同的答案。更正这是我的模板的外观,我按照您上面的内容重新编写了它,但仍然存在相同的错误。还尝试了使用空模板(如没有值,只有一些html文本)和仍然相同的错误
render: function() {
        $(this.el).html(_.template(contactTemplate, this.model.toJSON()));
        return this;
    }