最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2: What does binding event to 0 mean? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jan 12, 2017 at 8:37 Hitesh KumarHitesh Kumar 3,7089 gold badges48 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11
(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.

It was not clear for me as well, because I thought that Angular triggers the change detection on any asynchronous event. For example Angular University states that:

The following frequently used browser mechanisms are patched to support change detection:

  • all browser events (click, mouseover, keyup, etc.)
  • setTimeout() and setInterval()
  • Ajax requests

But it's not a whole truth, because official documentation says that:

Angular updates the bindings (and therefore the screen) only if the app does something in response to asynchronous events, such as keystrokes. This example 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.

So apparently an asynchronous event must be handled in the application in order to trigger the change detection, hence (keyup)="0"

发布评论

评论列表(0)

  1. 暂无评论