简体   繁体   English

在rivets.js中动态添加组件不起作用

[英]Add components dynamically in rivets.js not working

I get the data from server and loop the data value then I will add the rivets template in HTML DOM according to the data 我从服务器获取数据并循环数据值,然后根据数据在HTML DOM中添加铆钉模板

this is my HTML code 这是我的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> 

this is my javascript code 这是我的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 }) 

When I run it in chrome I found that htitle tag is created but template is not add why? 当我在chrome中运行它时,我发现创建了htitle标签,但未添加模板,为什么?

this is code in chrome debugger 这是chrome调试器中的代码

 <h_title mod-attr="{&quot;type&quot;:&quot;htitle&quot;,&quot;content&quot;:&quot;xxxxxx&quot;}"></h_title> 

it should be 它应该是

 <h_title mod-attr="{&quot;type&quot;:&quot;htitle&quot;,&quot;content&quot;:&quot;xxxxxx&quot;}"> <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM