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

查看:55
本文介绍了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天全站免登陆