Javascript Backbone.js视图中声明的变量';s的渲染方法似乎不存在?
当我像这样实例化视图对象时:Javascript Backbone.js视图中声明的变量';s的渲染方法似乎不存在?,javascript,backbone.js,single-page-application,Javascript,Backbone.js,Single Page Application,当我像这样实例化视图对象时: app.MyCollectionView = Backbone.View.extend({ el: "#my-element", template: _.template($(#my-view-template).html()), render: function(){ this.$el.append(this.template({ "myTemplateVar": this.html_string })); var
app.MyCollectionView = Backbone.View.extend({
el: "#my-element",
template: _.template($(#my-view-template).html()),
render: function(){
this.$el.append(this.template({
"myTemplateVar": this.html_string
}));
var html_string = "<p>Some stuff here</p>";
}
});
app.MyCollectionView=Backbone.View.extend({
el:“我的元素”,
模板:35;.template($(#我的视图模板).html()),
render:function(){
this.$el.append(this.template({
“myTemplateVar”:this.html\u字符串
}));
var html_string=“这里有些东西””;
}
});
变量“html_string”未生效。视图将以空的“myTemplateVar”呈现。但是,如果我将“html_字符串”声明为视图参数,则一切正常。上面的代码有什么问题?调用函数时,
此
设置为视图。html\u字符串
变量是渲染函数的属性,而不是视图。调用函数时,此
设置为视图。您的html\u字符串
变量是渲染函数的属性,而不是视图的属性。修复代码:
在此代码中this
Inthis.html\u字符串
指的是传递给this.template
方法的对象。固定代码:
在此代码this
Inthis.html\u字符串
中,this指的是传递给this.template
方法的对象
app.MyCollectionView = Backbone.View.extend({
el: "#my-element",
template: _.template($('#my-view-template').html()),
render: function(){
var html_string = "<p>Some stuff here</p>";
this.$el.append(this.template({
"myTemplateVar": html_string
}));
}
});
this.$el.append(this.template({
"myTemplateVar": this.html_string
}));