Angular2 - Keyup 需要澄清 [英] Angular2 - Keyup require a clarification

查看:23
本文介绍了Angular2 - Keyup 需要澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有条件地添加一个类.当用户输入内容时,我正在检查值,因此我正在添加类名.它工作正常.

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.

但它仅在 (keyup)='0' 集上更新 - 在 keyup 上设置一些值.这不像这里的 angular 1.

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

所以有人解释一下为什么我们在这里设置 (keyup)=0 吗?它对我们有什么作用?

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

这是我的代码:

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

推荐答案

官方文档

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

在官方文档中查找.

除非我们绑定到一个事件,否则这根本不起作用.

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

如果我们这样做,Angular 只会更新绑定(以及屏幕)响应诸如按键之类的异步事件.

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

这就是为什么我们将 keyup 事件绑定到一个语句,该语句...没有.我们绑定到数字 0,这是我们可以使用的最短语句考虑到.这就是让 Angular 开心的全部.我们说会聪明点!

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!

这篇关于Angular2 - Keyup 需要澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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