[英]Dynamically added paper-button is not rendered correctly
I have the following html: 我有以下html:
<paper-button raised>Static</paper-button>
<script>
var button = document.createElement('paper-button');
button.textContent = "dynamic";
button.raised = true;
document.body.appendChild(button);
</script>
If I add the paper-button statically, it renders normally, but I have do allmost the exact same thing dynamically, I don't get any animations. 如果我静态添加纸质按钮,它会正常渲染,但我动态完成所有相同的操作,我没有动画。
Is there something special I need to do if I add the paper-button dynamically ? 如果我动态添加纸质按钮,是否需要做一些特别的事情?
see: http://jsbin.com/rararurotu/1/edit?html,output 请参阅: http : //jsbin.com/rararurotu/1/edit?html,output
You need to set the textContent
using the Polymer.dom api. 您需要使用Polymer.dom api设置
textContent
。
The following code will work: 以下代码将起作用:
<paper-button raised>static</paper-button>
<script>
var button = document.createElement('paper-button');
button.raised = true;
Polymer.dom(button).textContent = 'dynamic';
document.body.appendChild(button);
</script>
see: http://jsbin.com/gexifaxaqi/1/edit?html,output 请参阅: http : //jsbin.com/gexifaxaqi/1/edit?html,output
In Polymer 1.0, there are a couple of ways to do this. 在Polymer 1.0中,有几种方法可以做到这一点。
document.createElement
) document.createElement
) Update: I think @Kasper's response is a better approach when using Polymer.dom because it allows us to insert textContent directly instead of via an internal class. 更新:我认为使用Polymer.dom时@ Kasper的响应是一种更好的方法,因为它允许我们直接插入textContent而不是通过内部类。
<!-- my-dom-element.html -->
<link href="bower_components/polymer/polymer.html" rel="import">
<link href="bower_components/paper-button/paper-button.html" rel="import">
<dom-module id="my-dom-element">
<template>
<div>
<paper-button raised>button 1</paper-button>
</div>
</template>
<script>
Polymer({
is: 'my-dom-element',
ready: function () {
var button = document.createElement('paper-button');
button.raised = true;
button.querySelector('.content').textContent = 'button 2';
Polymer.dom(this.root).appendChild(button);
}
});
</script>
</dom-module>
See Polymer.dom
for more info. 有关详细信息,请参阅
Polymer.dom
。
Here we use Polymer's native language to create the button element based on a condition (in this case, a property value). 这里我们使用Polymer的本地语言根据条件(在本例中为属性值)创建button元素。
<!-- my-conditional-dom-element.html -->
<link href="bower_components/polymer/polymer.html" rel="import">
<link href="bower_components/paper-button/paper-button.html" rel="import">
<dom-module id="my-conditional-dom-element">
<template>
<div>
<template is="dom-if" if="{{success}}">
<paper-button raised>
Conditional Button
</paper-button>
</template>
</div>
</template>
<script>
Polymer({
is: 'my-conditional-dom-element',
properties: {
success: {
type: Boolean,
value: true
}
}
});
</script>
</dom-module>
See helper elements for more info. 有关详细信息,请参阅辅助元素 。
My personal take is that Polymer's DSL for creating components is fairly clean and, where possible, it is good to take advantage of that. 我个人认为,Polymer用于创建组件的DSL相当干净,并且在可能的情况下,利用它是很好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.