Angular 2事件绑定使用插值在模板内不起作用 [英] Angular 2 event binding use interpolation doesn't work inside template

查看:126
本文介绍了Angular 2事件绑定使用插值在模板内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的单击事件绑定

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");
    }
}

当然,如果将({{eventStr}})更改为(click),它将起作用.我尝试了许多类似(${eventStr}) ,(eventStr)的方法.我不了解,在(click)="do-log()"内,此 click 不是字符串还是其他类型? 我想使用eventStr而不是click,因为我想更改事件类型为dynamic.滑动.

of course it will work if change ({{eventStr}}) to (click) .I try many way like (${eventStr}) ,(eventStr). I don't understand ,inside (click)="do-log()" ,this click is not string or what other type? I want to use eventStr instead of click because I want to change event type dynamic,e.g. swipe.

更新: 我知道@HostBindingrenderer.listen有两种方式,但是我有不同的用例,并找到了这样的方式:在父组件模板中:

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:

 <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>

当然是@Input eventType:string.它不起作用.我猜想eventType不是字符串而是事件对象,实际上是(eventType)==("swipe") or==("click"),不等于(swipe)(click).因此,有没有一种方法可以让(eventType)==(swipe)进入模板?

of course with @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). so is there one way to let (eventType)==(swipe) inside template?

推荐答案

您不能这样做.

绑定表达式必须是可静态分析的,因此您不能动态生成它们.锚点也一样(此处讨论).

Binding expressions have to be statically analyzable, so you cannot generate them dynamically. Same goes for anchors (discussion here).

这篇关于Angular 2事件绑定使用插值在模板内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆