Professional Documents
Culture Documents
1 to 2 Quick Reference ts COOKBOOK
There are many conceptual and syntactical differences between Angular 1 and Angular 2. This page provides a quick guide to
some common Angular 1 syntax and its equivalent in Angular 2.
Contents
This page covers:
Modules/controllers/components - modules in Angular 2 are slightly different from modules in Angular 1, and controllers are
components in Angular 2.
Template basics
Templates are the user-facing part of an Angular application and are written in HTML. The following table lists some of the key
Angular 1 template features with their equivalent Angular 2 template syntax.
Angular 1 Angular 2
Bindings/interpolation Bindings/interpolation
In Angular 1, an expression in curly braces denotes one- In Angular 2, a template expression in curly braces still
way binding. This binds the value of the element to a denotes one-way binding. This binds the value of the
property in the controller associated with this template. element to a property of the component. The context of
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 1/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
When using the controller as syntax, the binding is the binding is implied and is always the associated
pre xed with the controller alias ( vm or $ctrl ) component, so it needs no reference variable.
because you have to be speci c about the source of
the binding. For more information, see the Interpolation section of
the Template Syntax page.
Filters Pipes
Back to top
Template directives
Angular 1 provides more than seventy built-in directives for templates. Many of them aren't needed in Angular 2 because of its
more capable and expressive binding system. The following are some of the key Angular 1 built-in directives and their equivalents
in Angular 2.
Angular 1 Angular 2
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 2/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
ng-app Bootstrapping
bootstrapping. '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
ng-class ngClass
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 3/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
In Angular 1, the ng-click directive allows you Angular 1 event-based directives do not exist in Angular 2. Rather,
to specify custom behavior when an element is de ne one-way binding from the template view to the component
clicked. using event binding.
In the rst example, when the user clicks the For event binding, de ne the name of the target event within
button, the toggleImage() method in the parenthesis and specify a template statement, in quotes, to the right
controller referenced by the vm of the equals. Angular 2 then sets up an event handler for the target
controller as alias is executed. event. When the event is raised, the handler executes the template
statement.
The second example demonstrates passing in
the $event object, which provides details about In the rst example, when a user clicks the button, the
the event to the controller. toggleImage() method in the associated component is executed.
For more information, see the Event Binding section of the Template
Syntax page.
<div ng-controller="MovieListCtrl as
vm">
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 4/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
The ng-href directive allows Angular 1 to Angular 2, uses property binding; there is no built-in href directive.
preprocess the href property so that it can Place the element's href property in square brackets and set it to a
replace the binding expression with the quoted template expression.
appropriate URL before the browser fetches from
that URL. For more information on property binding, see Template Syntax.
In Angular 1, the ng-href is often used to In Angular 2, href is no longer used for routing. Routing uses
activate a route as part of navigation. routerLink , as shown in the third example.
ng-if *ngIf
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 5/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
In this example, the table element is removed In this example, the table element is removed from the DOM
from the DOM unless the movies array has a unless the movies array has a length.
length greater than zero.
The (*) before ngIf is required in this example. For more
information, see Structural Directives.
ng-model ngModel
ng-repeat *ngFor
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 6/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
In Angular 1, the ng-show directive shows or Angular 2, uses property binding; there is no built-in show directive.
hides the associated DOM element, based on an For hiding and showing elements, bind to the HTML hidden
expression. property.
In this example, the div element is shown if the To conditionally display an element, place the element's hidden
favoriteHero variable is truthy. property in square brackets and set it to a quoted template
expression that evaluates to the opposite of show.
ng-style ngStyle
In the example, the color style is set to the Angular 2 also has style binding, which is good way to set a single
current value of the colorPreference variable. style. This is shown in the second example.
ng-switch ngSwitch
Back to top
Filters/pipes
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 8/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
Angular 2 pipes provide formatting and transformation for data in our template, similar to Angular 1 lters. Many of the built-in
lters in Angular 1 have corresponding pipes in Angular 2. For more information on pipes, see Pipes.
Angular 1 Angular 2
currency currency
date date
lter none
For performance reasons, no comparable pipe exists in
<tr ng-repeat="movie in movieList |
Angular 2. Do all your ltering in the component. If you
filter: {title:listFilter}">
need the same ltering code in several templates,
Selects a subset of items from the de ned collection, consider building a custom pipe.
based on the lter criteria.
json json
limitTo slice
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 9/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
lowercase lowercase
number number
orderBy none
For performance reasons, no comparable pipe exists in
<tr ng-repeat="movie in movieList |
Angular 2. Instead, use component code to order or
orderBy : 'title'">
sort results. If you need the same ordering or sorting
Displays the collection in the order speci ed by the code in several templates, consider building a custom
expression. In this example, the movie title orders the pipe.
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 10/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
movieList.
Back to top
Modules/controllers/components
In both Angular 1 and Angular 2, Angular modules help you organize your application into cohesive blocks of functionality.
In Angular 1, you write the code that provides the model and the methods for the view in a controller. In Angular 2, you build a
component.
Because much Angular 1 code is in JavaScript, JavaScript code is shown in the Angular 1 column. The Angular 2 code is shown
using TypeScript.
Angular 1 Angular 2
IIFE none
You don't need to worry about this in Angular 2 because
(function () {
you use ES 2015 modules and modules handle the
...
}()); namespacing for you.
In Angular 1, you often de ned an immediately invoked For more information on modules, see Architecture
function expression (or IIFE) around your controller Overview.
code. This kept your controller code out of the global
namespace.
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 11/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
angular @Component({
.module("movieHunter") moduleId: module.id,
.controller("MovieListCtrl", selector: 'movie-list',
["movieService", templateUrl: 'movie-
MovieListCtrl]); list.component.html',
styleUrls: [ 'movie-
Angular 1, has code in each controller that looks up an list.component.css' ],
appropriate Angular module and registers the controller })
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 12/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
Back to top
Style sheets
Style sheets give your application a nice look. In Angular 1, you specify the style sheets for your entire application. As the
application grows over time, the styles for the many parts of the application merge, which can cause unexpected results. In
Angular 2, you can still de ne style sheets for your entire application. But now you can also encapsulate a style sheet within a
speci c component.
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 13/14
11/20/2016 Angular 1 to 2 Quick Reference ts COOKBOOK
Angular 1 Angular 2
StyleUrls
In Angular 2, you can use the styles or styleUrls
property of the @Component metadata to de ne a
style sheet for a particular component.
styleUrls: [ 'movie-list.component.css'
],
Back to top
https://angular.io/docs/ts/latest/cookbook/a1a2quickreference.html 14/14