Angular2:绑定事件到0是什么意思? [英] Angular2: What does binding event to 0 mean?

查看:192
本文介绍了Angular2:绑定事件到0是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Angular 2 来自模板参考变量rel =nofollow noreferrer>官方指南。我遇到了以下代码。

  @Component({
selector:'loop-back',
template:`
< input #box(keyup)=0>
< p> {{box.value}}< / p>
`
})
export class LoopbackComponent {}

正如你在模板中看到的那样 keyup 事件绑定到 0 (keyup)=0。当事件绑定到一个数字时,我不明白这意味着什么。在文档中,它表示


代码将 keyup 事件绑定到数字 0 ,最短的模板语句可能。虽然该声明没有任何用处,但它满足了Angular的要求,因此Angular将会更新屏幕。


找到有关绑定事件到数字的任何解释。有人可以帮我吗谢谢。

解决方案

 (keyup)=0
返回0
,这是相当等价的不做任何事。没有更短的表达方式,除了没有添加任何事件绑定。



在该示例中使用事件绑定来导致更改检测运行,这是默认情况下每次调用一个事件处理程序时运行。



没有事件绑定,没有事件处理程序,Angular将不会运行更改检测,这将导致 {{box.value}} to not 更新值。


I am learning Angular 2 from the official guide. I came across the following piece of code.

    @Component({
  selector: 'loop-back',
  template: `
    <input #box (keyup)="0">
    <p>{{box.value}}</p>
  `
})
export class LoopbackComponent { }

As you see in the template keyup event is bound to 0, (keyup)="0". I don't understand what it means when an event is bound to a number. In doc it says that

code binds the keyup event to the number 0, the shortest template statement possible. While the statement does nothing useful, it satisfies Angular's requirement so that Angular will update the screen.

I delved over internet also but could not find any explanation regarding to binding events to number. Can anyone please help me on this? Thanks.

解决方案

(keyup)="0"

means, when that event happens, then return 0, which is quite equivalent to "do nothing". There is no shorter way of expressing that, except not adding any event binding at all.

The event binding is used in that example to cause change detection to run, which is by default run every time an event handler was called.

Without the event binding, there is no event handler and Angular won't run change detection, which will cause {{box.value}} to not update the value.

这篇关于Angular2:绑定事件到0是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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