# Jumping from Angular1 to Angular

VonRickroll
1,344 views

### Open Source Your Knowledge, Become a Contributor

Let's take a look at how Data-Binding works in Angular. Displaying stuff in your template is still done with the {{ doubleCurlyBrackets }} notation. Other than that, passing variables and events to components happens with different notations.

## Variables (input)

You use input binding to pass the value of a variable to a child component. In Angular1 you used to do something like this:

<directive child-name="scopeVariable">


But now with Angular, you will use the [] square bracket notation, like so:

<directive [childName]="scopeVariable">


This binds value. It actually binds to an attribute of the child component that has been marked as an input with the @Input decorator.

@Component({
...
})
class Compo {
@Input()
public param;
}


Let's try it out, shall we?

Input demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Component, Input } from '@angular/core';
@Component({
selector: 'box',
template: 
<p class="box">
I am a box of {{things.amount}} {{things.item}}
</p>

})
export class BoxComponent {
@Input()
public things: any;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

## Events (output)

Two-ways binding doesn't really exist in Angular anymore. Now, when you want to send data upward to the parent component, you need to use events. Events are defined with the () notation like this:

<directive (theEvent)="theScopeFunction(\$event)">


Guess what? A bunch of events already exist, known as the HTML DOM events. The same ones you used to prepend ng- to. You can now use what used to be ngClick like this:

<button (click)="clickButton()">


You can also define your own output events using the EventEmitter system. Let's say you want to create a highlight event that is triggered when the user highlights an item in a list. First, you define an event emitter with the @Output decorator, use it to send events with the next method. It will then be exposed to the parent component via the (highlight) attribute (note the use of the event syntax ()). Here's a simple snippet:

Output demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'list',
template: 
<div class="list">
<p *ngFor="let item of list">
Element: {{item}}
<button (click)="onClick(item)">Highlight</button>
</p>
</div>

})
export class ListComponent {
public list = ['tech.io', 'is', 'so', 'cool'];
@Output()
public highlight = new EventEmitter();
public onClick(item) {
this.highlight.next(item);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

### What happened to...

#### ng-class and angular-specific directives?

ngClass, and others such as ngStyle are bound like regular variables,

<directive [ngClass]="scopeVariable">


#### ng-model?

ngModel is a bit different because it is both downward and upward, both input and output, right? No worries, Angular got you covered:

<input [(ngModel)]="scopeVariable">

Open Source Your Knowledge: become a Contributor and help others learn.