Angular2 - Keyup require a clarification

拜拜、爱过 提交于 2021-01-27 08:15:48

问题


In my app, conditionally i am adding a class. and when the user enter something i am checking the value and accordingly i am adding the class name. it works fine.

but it only updates on set of (keyup)='0' - setting some value on keyup. this is not like angular 1 here.

so any one explain me why do we set the (keyup)=0 here? and what it do for us?

here is my code :

import {Component} from "angular2/core"

@Component({

    selector : 'my-component',

    template : `
                <h2>My Name is: {{name}} 
                    <span [class.is-awesome]="formReplay.value === 'yes' ">So good</span>
                </h2>
                <input type="text" #formReplay (keyup)="0" />
                `,

    styles  : [`

        .is-awesome{
            color:green;
        }

    `]

})

export class MyComponent { 
    name = "My Name";   
}

回答1:


Offical docs

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

look for this in offical docs.

This won't work at all unless we bind to an event.

Angular only updates the bindings (and therefore the screen) if we do something in response to asynchronous events such as keystrokes.

That's why we bind the keyup event to a statement that does ... well, nothing. We're binding to the number 0, the shortest statement we can think of. That is all it takes to keep Angular happy. We said it would be clever!



来源:https://stackoverflow.com/questions/36001757/angular2-keyup-require-a-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!