[英]Angular 2 event binding use interpolation doesn't work inside template
I have simple click event binding我有简单的点击事件绑定
@Component({template:` <div ({{eventStr}})="do-log()">DoLog</div>`})
export class AppComponent {
eventStr:string="click";
do-log(){
console.log("ckp");
}
}
of course it will work if change ({{eventStr}})
to (click)
.I try many way like (${eventStr}) ,(eventStr)
.当然,如果将
({{eventStr}})
更改为(click)
,它会起作用。我尝试了很多方法,例如(${eventStr}) ,(eventStr)
。 I don't understand ,inside (click)="do-log()"
,this click is not string or what other type?我不明白,在
(click)="do-log()"
,这个点击不是字符串还是其他什么类型? I want to use eventStr
instead of click
because I want to change event type dynamic,eg swipe.我想使用
eventStr
而不是click
因为我想动态更改事件类型,例如滑动。
Update: I know there are two way @HostBinding
and renderer.listen
, but I have different use case and find some way like this: in parent component template like:更新:我知道有两种方式
@HostBinding
和renderer.listen
,但我有不同的用例并找到了这样的方式:在父组件模板中,如:
<child-cmp eventType="swipe">Swipe</child-cmp><child-cmp eventType="click">Click</child-cmp>
and in child component template like:并在子组件模板中,如:
<div><button (eventType)="dosomething_accordingTo_diff_touch_events()"></button></div>
of course with @Input eventType:string
.当然使用
@Input eventType:string
。 It doesn't work.它不起作用。 I guess if
eventType
is not a string but an event object, in fact (eventType)==("swipe") or==("click")
, not equal to (swipe)
or (click)
.我猜如果
eventType
不是字符串而是事件对象,实际上(eventType)==("swipe") or==("click")
不等于(swipe)
or (click)
。 so is there one way to let (eventType)==(swipe)
inside template?那么有没有一种方法可以让
(eventType)==(swipe)
进入模板?
You can't do that.你不能那样做。
Binding expressions have to be statically analyzable, so you cannot generate them dynamically.绑定表达式必须是静态可分析的,因此您不能动态生成它们。 Same goes for anchors ( discussion here ).
锚点也是如此( 这里讨论)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.