Javascript Polymer 1.0-绑定事件处理程序而不必创建自定义元素
我在index.html中有Javascript Polymer 1.0-绑定事件处理程序而不必创建自定义元素,javascript,polymer,Javascript,Polymer,我在index.html中有。如果不创建自定义元素,如何为其创建事件侦听器并将其绑定到单独的文件中?类似于Angular的控制器,您可以绑定元素而无需创建指令 您将使用“dom-bind”模板(也称为“自动绑定模板”) 编辑:有时您需要等待模板绑定后再进行操作。然后等待“dom更改”事件 app.addEventListener('dom-change', function() { // auto-binding template is ready. }); 这里还有另一种解释 事件侦听器
。如果不创建自定义元素,如何为其创建事件侦听器并将其绑定到单独的文件中?类似于Angular的控制器,您可以绑定元素而无需创建指令 您将使用“dom-bind”模板(也称为“自动绑定模板”)
编辑:有时您需要等待模板绑定后再进行操作。然后等待“dom更改”事件
app.addEventListener('dom-change', function() {
// auto-binding template is ready.
});
这里还有另一种解释 事件侦听器设置
<dom-module id="x-custom">
<template>
<div>I will respond</div>
<div>to a tap on</div>
<div>any of my children!</div>
<div id="special">I am special!</div>
</template>
<script>
Polymer({
is: 'x-custom',
listeners: {
'tap': 'regularTap',
'special.tap': 'specialTap'
},
regularTap: function(e) {
alert("Thank you for tapping");
},
specialTap: function(e) {
alert("It was special tapping");
}
});
</script>
</dom-module>
我会回应的
敲打
我的孩子们!
我很特别!
聚合物({
是:“x-custom”,
听众:{
"tap":"regular tap",,
“special.tap”:“specialTap”
},
常规点击:功能(e){
警惕(“感谢您点击”);
},
特殊地图:功能(e){
警惕(“这是一次特殊的窃听”);
}
});
带注释的事件侦听器设置
<dom-module id="x-custom">
<template>
<button on-tap="handleTap">Kick Me</button>
</template>
<script>
Polymer({
is: 'x-custom',
handleTap: function() {
alert('Ow!');
}
});
</script>
</dom-module>
踢我
聚合物({
是:“x-custom”,
handleTap:function(){
警惕(‘噢!’);
}
});
<dom-module id="x-custom">
<template>
<div>I will respond</div>
<div>to a tap on</div>
<div>any of my children!</div>
<div id="special">I am special!</div>
</template>
<script>
Polymer({
is: 'x-custom',
listeners: {
'tap': 'regularTap',
'special.tap': 'specialTap'
},
regularTap: function(e) {
alert("Thank you for tapping");
},
specialTap: function(e) {
alert("It was special tapping");
}
});
</script>
</dom-module>
<dom-module id="x-custom">
<template>
<button on-tap="handleTap">Kick Me</button>
</template>
<script>
Polymer({
is: 'x-custom',
handleTap: function() {
alert('Ow!');
}
});
</script>
</dom-module>