Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - Keyup require a clarification

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";   
}
like image 822
user2024080 Avatar asked Mar 15 '16 02:03

user2024080


1 Answers

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!

like image 86
Nikhil Shah Avatar answered Nov 17 '22 12:11

Nikhil Shah