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.
See the Angular 2 syntax in this
Contents
This page covers:
Template basics - binding and local variables.
Template directives - built-in directives
ngIfandngClass.Filters/pipes - built-in filters, known as pipes in Angular 2.
Modules/controllers/components - modules in Angular 2 are slightly different from modules in Angular 1, and controllers are components in Angular 2.
Style sheets - more options for CSS than in Angular 1.
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/interpolationIn Angular 1, an expression in curly braces denotes one-way binding. This binds the value of the element to a property in the controller associated with this template. When using the | Bindings/interpolationIn Angular 2, a template expression in curly braces still denotes one-way binding. This binds the value of the element to a property of the component. The context of the binding is implied and is always the associated component, so it needs no reference variable. For more information, see the Interpolation section of the Template Syntax page. |
FiltersTo filter output in Angular 1 templates, use the pipe character (|) and one or more filters. This example filters the | PipesIn Angular 2 you use similar syntax with the pipe (|) character to filter output, but now you call them pipes. Many (but not all) of the built-in filters from Angular 1 are built-in pipes in Angular 2. For more information, see the heading Filters/pipes below. |
Local variablesHere, | Input variablesAngular 2 has true template input variables that are explicitly defined using the For more information, see the ngFor micro-syntax section of the Template Syntax page. |
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 |
|---|---|
ng-appThe application startup process is called bootstrapping. Although you can bootstrap an Angular 1 app in code,
many applications bootstrap declaratively with the | Bootstrappingmain.tsapp.module.tsAngular 2 doesn't have a bootstrap directive.
To launch the app in code, explicitly bootstrap the application's root module ( For more information see the Setup page. |
ng-classIn Angular 1, the In the first example, the You can specify multiple classes, as shown in the second example. | ngClassIn Angular 2, the In the first example, the You can specify multiple classes, as shown in the second example. Angular 2 also has class binding, which is a good way to add or remove a single class, as shown in the third example. For more information see the Attribute, Class, and Style Bindings section of the Template Syntax page. |
ng-clickIn Angular 1, the In the first example, when the user clicks the button, the The second example demonstrates passing in the | bind to the
|
ng-controllerIn Angular 1, the | Component decoratorIn Angular 2, the template no longer specifies its associated controller. Rather, the component specifies its associated template as part of the component class decorator. For more information, see Architecture Overview. |
ng-hideIn Angular 1, the | bind to the
|
ng-hrefThe In Angular 1, the Routing is handled differently in Angular 2. | bind to the
|
ng-ifIn Angular 1, the In this example, the | *ngIfThe In this example, the The (*) before |
ng-modelIn Angular 1, the | ngModelIn Angular 2, two-way binding is denoted by For more information on two-way binding with ngModel, see Template Syntax. |
ng-repeatIn Angular 1, the In this example, the table row ( | *ngForThe Notice the other syntax differences:
The (*) before For more information, see Structural Directives. |
ng-showIn Angular 1, the In this example, the | bind to the
|
ng-srcThe | bind to the
|
ng-styleIn Angular 1, the In the example, the | ngStyleIn Angular 2, the In the first example, the Angular 2 also has style binding, which is good way to set a single style. This is shown in the second example. For more information on style binding, see Template Syntax. For more information on the ngStyle directive, see Template Syntax. |
ng-switchIn Angular 1, the In this example, if | ngSwitchIn Angular 2, the In this example, if The (*) before For more information on the ngSwitch directive, see Template Syntax. |
Filters/pipes
Angular 2 pipes provide formatting and transformation for data in our template, similar to Angular 1 filters. Many of the built-in filters in Angular 1 have corresponding pipes in Angular 2. For more information on pipes, see Pipes.
| Angular 1 | Angular 2 |
|---|---|
currencyFormats a number as a currency. | currencyThe Angular 2 |
dateFormats a date to a string based on the requested format. | dateThe Angular 2 |
filterSelects a subset of items from the defined collection, based on the filter criteria. | noneFor performance reasons, no comparable pipe exists in Angular 2. Do all your filtering in the component. If you need the same filtering code in several templates, consider building a custom pipe. |
jsonConverts a JavaScript object into a JSON string. This is useful for debugging. | jsonThe Angular 2 |
limitToSelects up to the first parameter (2) number of items from the collection starting (optionally) at the beginning index (0). | sliceThe |
lowercaseConverts the string to lowercase. | lowercaseThe Angular 2 |
numberFormats a number as text. | numberThe Angular 2 Angular 2 also has a |
orderByDisplays the collection in the order specified by the expression. In this example, the movie title orders the movieList. | noneFor performance reasons, no comparable pipe exists in Angular 2. Instead, use component code to order or sort results. If you need the same ordering or sorting code in several templates, consider building a custom pipe. |
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 |
|---|---|
IIFEIn Angular 1, you often defined an immediately invoked function expression (or IIFE) around your controller code. This kept your controller code out of the global namespace. | noneYou don't need to worry about this in Angular 2 because you use ES 2015 modules and modules handle the namespacing for you. For more information on modules, see Architecture Overview. |
Angular modulesIn Angular 1, an Angular module keeps track of controllers, services, and other code. The second argument defines the list of other modules that this module depends upon. | Angular modulesAngular 2 modules, defined with the
For more information on modules, see Angular Modules. |
Controller registrationAngular 1, has code in each controller that looks up an appropriate Angular module and registers the controller with that module. The first argument is the controller name. The second argument defines the string names of all dependencies injected into this controller, and a reference to the controller function. | Component DecoratorAngular 2, adds a decorator to the component class to provide any required metadata. The Component decorator declares that the class is a component and provides metadata about that component such as its selector (or tag) and its template. This is how you associate a template with code, which is defined in the component class. For more information, see the Components section of the Architecture Overview page. |
Controller functionIn Angular 1, you write the code for the model and methods in a controller function. | Component classIn Angular 2, you create a component class. NOTE: If you are using TypeScript with Angular 1, you must use the For more information, see the Components section of the Architecture Overview page. |
Dependency injectionIn Angular 1, you pass in any dependencies as controller function arguments.
This example injects a To guard against minification problems, tell Angular explicitly
that it should inject an instance of the | Dependency injectionIn Angular 2, you pass in dependencies as arguments to the component class constructor.
This example injects a For more information, see the Dependency Injection section of the Architecture Overview. |
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 define style sheets for your entire application. But now you can also encapsulate a style sheet within a specific component.
| Angular 1 | Angular 2 |
|---|---|
Link tagAngular 1, uses a | Link tagIn Angular 2, you can continue to use the link tag to define the styles for your application in the StyleUrlsIn Angular 2, you can use the This allows you to set appropriate styles for individual components that won’t leak into other parts of the application. |