全,
我正在尝试传递一个简单的JSON对象(用模型尝试,但问题仍然相同)。
我用一个名为“技能”的变量初始化了我的视图,我想把它传递到我的模板中。然而,在模板内部,它说它是未定义的。
define([
'jquery',
'underscore',
'backbone',
'text!templates/skillsPopup.html', //for templates you should prepend the path with !text
'common'
], function ($, _, Backbone, SkillsTemplate, Common, Skills)
{
'use strict';
var FilterView = Backbone.View.extend({
el: $("#skillsPopup"), //set this to the element where the template will be rendered upon
skills :
[{
"skillCode": "Mechanical Engineer",
"skillDescription": "Mechanical Engineer"
},
{
"skillCode": "Air Conditioning Engineer",
"skillDescription": "Air Conditioning Engineer"
},
{
"skillCode": "Electrical Engineer",
"skillDescription": "Electrical Engineer"
}],
template: _.template(SkillsTemplate),
// setup the view to listen to its counterpart model for changes
initialize: function ()
{
this.render();
},
// render template onto the view
render: function ()
{
this.$el.html(this.skills);
return this;
}
});
return FilterView;
});模板
<div id="test">
<% console.log(skills); %>
</div> 误差
ReferenceError:技能没有定义 Console.log(技能);
我看过几个骨干教程,我看不出我犯了什么错误。
发布于 2015-05-20 14:50:56
您应该将上下文传递给模板,而不是jQuery的html方法:
this.$el.html(this.template({ skills: this.skills }));发布于 2015-05-20 14:50:27
您需要调用.template方法
this.$el.html(this.template({skills: this.skills}));_.template
https://stackoverflow.com/questions/30353106
复制相似问题