[英]Add components dynamically in rivets.js not working
我从服务器获取数据并循环数据值,然后根据数据在HTML DOM中添加铆钉模板
这是我的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标签,但未添加模板,为什么?
这是chrome调试器中的代码
<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>
我使用铆钉包括实现它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.