简体   繁体   English

Ember:动态渲染模板

[英]Ember: Dynamically render a template

I have a component. 我有一个组件。 Template of this component is "html data". 该组件的模板是“html数据”。 Those html data are composed by users and stored in database. 这些html数据由用户组成并存储在数据库中。 They are loaded with ajax. 它们装有ajax。

Normally, we just bind the component with html data. 通常,我们只是使用html数据绑定组件。 However, my "html data" also contains embedded ember component. 但是,我的“html数据”还包含嵌入式ember组件。 Ex: 例如:

<html> do something {{button action="abc"}} </html>.

So, what I have to do is to change dynamically the template of the component. 所以,我要做的是动态更改组件的模板。 I tried by updating the template. 我尝试更新模板。

this.template = Ember.Handlebars.compile(html);
this.rerender();

However, the component does not rerender with new template !! 但是,组件不会使用新模板重新渲染!!

Do you have an idea how to do this? 你知道怎么做吗?

Thank you in advance 先感谢您

Green 绿色

You just need to call Ember.set method while setting values for Ember's objects. 您只需要在设置Ember对象的值时调用Ember.set方法。 Use this.set('template', template) instead of this.template = template . 使用this.set('template', template)代替this.template = template

Demo (type new template into input and click on button): http://emberjs.jsbin.com/cuqofatisavu/1/edit?js,output 演示(在输入中键入新模板并单击按钮): http//emberjs.jsbin.com/cuqofatisavu/1/edit?js,output

Update (thx melc for commenting ): Set layout property instead of template if you need to change Ember.Component template. 更新(THX MELC的评论 ):设置layout属性,而不是template ,如果你需要改变Ember.Component模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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