首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据传递到视图模板在Backbone.js中是未定义的?

将数据传递到视图模板在Backbone.js中是未定义的?
EN

Stack Overflow用户
提问于 2015-05-20 14:47:50
回答 2查看 507关注 0票数 0

全,

我正在尝试传递一个简单的JSON对象(用模型尝试,但问题仍然相同)。

我用一个名为“技能”的变量初始化了我的视图,我想把它传递到我的模板中。然而,在模板内部,它说它是未定义的。

代码语言:javascript
复制
    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;
});

模板

代码语言:javascript
复制
<div id="test">
      <% console.log(skills); %>
</div> 

误差

ReferenceError:技能没有定义 Console.log(技能);

我看过几个骨干教程,我看不出我犯了什么错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-20 14:50:56

您应该将上下文传递给模板,而不是jQuery的html方法:

代码语言:javascript
复制
this.$el.html(this.template({ skills: this.skills }));
票数 1
EN

Stack Overflow用户

发布于 2015-05-20 14:50:27

您需要调用.template方法

代码语言:javascript
复制
this.$el.html(this.template({skills: this.skills}));

_.template

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30353106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档