Backbone.js Can';在模板中不显示模型信息

Backbone.js Can';在模板中不显示模型信息,backbone.js,Backbone.js,我想在模板中显示我的模型,但我有一个问题: Uncaught ReferenceError: title is not defined 这是我的密码: var NewsView2 = Backbone.View.extend({ NewsView: _.template(NewsView), initialize: function () { console.log(this); this.$el.html(t

我想在模板中显示我的模型,但我有一个问题:

Uncaught ReferenceError: title is not defined
这是我的密码:

    var NewsView2 = Backbone.View.extend({

    NewsView: _.template(NewsView),

    initialize: function () {
                    console.log(this);
        this.$el.html(this.NewsView());
        this.render();
    },
    render: function () {

        this.$el.html(this.NewsView());
        var html = this.NewsView(this.model.toJSON());
        this.$el.append(html);
        return this;
    }

});
初始化:

            var news2 = new News({
            author: "costam",
        });
        var widok2 = new NewsView2({model: news2});
和我的模板中的代码:

<html>
<head>
</head>
<body>
        <%= title %>
</body>


有人能帮我吗?我不知道该怎么做。

您的模型没有title属性。下划线模板正在抱怨,因为它试图在新闻模型中查找不存在的标题属性

您的主干视图:

var NewsView2 = Backbone.View.extend({

    initialize: function () {            
        this.render();
    },

    render: function () {            
        var template = _.template($("#yourTemplate").html())            
        var html = template(this.model.toJSON());
        this.$el.append(html);
        return this;
    }
});
您的型号:

var news2 = new News({ author: "costam", title : "Your title" });
模板:

<html>
<head>
</head>
<body>
   <script type="text/template" id="yourTemplate">
     <%= author %> // --> this should match/exist in your model 
     <%= title %> // --> this should match/exist in your model 
   </script>
</body>

//-->这应该匹配/存在于您的模型中
//-->这应该匹配/存在于您的模型中

注意:有不同的方法设置主干模板,如。

仍然不工作,相同的错误我注意到您的代码有问题。用基本方法解决一些问题,但我建议您阅读一些关于主干的教程或书籍。刚刚添加到模板this.autor/this.title和working:)ye,谢谢:)您只需向我展示另一种设置模板的方法:)什么是
新闻
以及它的构造函数在哪里?什么是新闻视图?模型在哪里?为什么你的模板是一个完整的HTML文档?为什么要在
初始化
渲染
中到处渲染?老实说,看起来你根本不知道自己在做什么。请先阅读一些教程/书籍和文档。我没有发送完整的代码,这就是为什么没有新闻等。我认为这是一个问题:)