How to dynamically create Polymer custom element after setting all attributes?

喜夏-厌秋 提交于 2019-12-25 03:52:00

问题


The problem is when I try to create custom element this way

var el= document.createElement('my-el');
el.setAttribute('tag-model', "[[myBinding]]");

it creates element without its attributes. How to construct custom element with all its attributes and then append to HTML to initilize them?

Thank you!


回答1:


See this

It basically says to do it like that:

var dynamicEl = document.createElement("my-element");
dynamicEl.setAttribute("id", "my-element-id");
dynamicEl.setAttribute("greeting", "Hello, Good Morning.");
document.body.appendChild(dynamicEl);

However, if you want to change properties directly, it wont work with setAttribute. You have to do it like that:

var dynamicEl = document.createElement("my-element");
dynamicEl.greeting = 'Waaazaaaa???';
document.body.appendChild(dynamicEl); 


来源:https://stackoverflow.com/questions/33102509/how-to-dynamically-create-polymer-custom-element-after-setting-all-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!