簡體   English   中英

_.template不起作用-bone.js

[英]_.template not working - backbone.js

嘗試使用Backbone.js通過模板從“視圖”中輸出一些值,但是列表項未顯示在網頁上,請查看一下並告知丟失的內容。 謝謝

<script type="text/template" id="myid">
    <li>  <%= value %> </li>
</script>
<script type="text/javascript">
(function($){
    ListView = Backbone.View.extend({
        tagName:'ul',
        initialize:function(){
            this.template = _.template($('#myid').html());
        },
        render:function(){
        this.$el.append(this.template({value:"Joe Blogg"}));
        return this;    
        }
    });
    $(document).ready(function(e) {
        myListView = new ListView();
    });
  })(jQuery);

請嘗試這個。

(function($){
    var ListView = Backbone.View.extend({
        tagName:'ul',
        $el: $(this.tagName),
        initialize:function(){
            this.template = _.template($('#myid').html());
        },
        render:function(){
            this.$el.append(this.template({value:"Joe Blogg"}));
            $('body').append(this.$el);
        }
    });
    $(document).ready(function(e) {
        var myListView = new ListView(); // call initialize()
        myListView.render(); // call render()
    });
})(jQuery);

我使用jQuery.js,Underscore.js和Backbone.js。

並始終使用“ var”。

嘗試這個

   this.$el.empty();
   this.$el.html(this.template(this.model.attributes));

DOM ready事件中,調用.render()方法

$(document).ready(function(e) {
     myListView = new ListView();
     myListView.render(); 
 });

您需要調用render ,然后將視圖的el實際添加到頁面的某處。

$(document).ready(function(e) {
   var myListView = new ListView();
   $("body").append(myListView.render().el); 
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM