0
我从服务器获取与循环中的数据值 数据,然后我会在HTML DOM根据数据在不工作
加上铆钉模板rivets.js动态添加组件,这是我的HTML代码
<div rv-each-mod='not.module'> <div rv-templatemod="mod"></div> </div> <template id="h_title"> <div class="notice-tp notice-i"> <p class="notice-tp-t first_font_size second_color">{modinfo}</p> </div> </template>
这是我的javascript代码
function templateViewModel(attributes) { this.modinfo= attributes.modAttr; console.log(this.modinfo) } rivets.binders.templatemod = function(el, mod) { var element=document.createElement(mod.type); //example mod.type = htitle element.setAttribute("mod-attr",JSON.stringify(mod)) el.parentNode && el.parentNode.appendChild(element) } rivets.components["htitle"] = { template : function(){ return $api.html($api.dom("#h_title")); }, initialize: function(el, attributes) { return new templateViewModel(attributes); } } rivets.bind($api.dom("#rivets-bind"),{ not:notice })
当我在chrome中运行它时,发现创建了htitle标签,但模板没有添加为什么?
这是铬调试
<h_title mod-attr="{"type":"htitle","content":"xxxxxx"}"></h_title>
代码应该是
<h_title mod-attr="{"type":"htitle","content":"xxxxxx"}"> <div class="notice-tp notice-i"> <p class="notice-tp-t first_font_size second_color">{modinfo}</p> </div> </h_title>