简体   繁体   English

动态更改点击属性

[英]dynamically change on-tap attribute

what i am trying to do is change the function that on-tap calls based on spa route. 我正在尝试做的是基于spa路线更改点击呼叫的功能。 here is what i have tried (working inside a custom element) 这是我尝试过的(在自定义元素中工作)

this.$.menuButton.attr['on-tap'] = "{{newFunction}}";

also

this.$.menuButton.on-tap = "{{newFunction}}";

as well as many other attempts none have worked. 以及许多其他尝试都没有奏效。 how do i change the value on a attribute that contains a - in the name? 如何更改名称中包含-的属性的值? if this isn't doable what would be the recommended method for changing the on-tap function dynamically? 如果这不可行,那么动态改变点按功能的推荐方法是什么?

If you want to go the declarative route, you can still use on-* handlers and data-binding to flip the handler: 如果要使用声明式路由,仍然可以使用on-*处理程序和数据绑定来翻转处理程序:

<p on-tap="{{handler}}">Tap me to see what handler is set.</p>
<button on-click="{{setHandler}}" data-handler="oneHandler">Set Handler 1</button>

ready: function() {
  this.setHandler();
},
setHandler: function(e, detail, sender) {
  this.handler = !sender ? this.defaultHandler :  
                           this[sender.dataset.handler];
},
defaultHandler: function() {
  alert('default handler');
},
oneHandler: function() {
   alert('one handler');
}

http://jsbin.com/forame/1/edit http://jsbin.com/forame/1/edit

Instead of the on-* declarative event mapping, you should be using event listeners. 您应该使用事件侦听器,而不是on- *声明性事件映射。

this.addEventListener('tap', this.myFunction);
this.addEventListener('tap', this.newFunction);

It's basically the same thing. 基本上是同一回事。 If you need (event, detail, sender) arguments, though, you have to pass them yourself. 但是,如果需要(event, detail, sender)参数,则必须自己传递它们。

API Reference API参考

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

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