Angular is the name for the Angular of today and tomorrow. AngularJS is the name for all v1.x versions of Angular.
This guide helps you transition from AngularJS to Angular by mapping AngularJS syntax to the equivalent Angular syntax.
See the Angular syntax in this
Contents
Template basics—binding and local variables.
Template directives—built-in directives
ngIf
andngClass
.Filters/pipes—built-in filters, known as pipes in Angular.
Modules/controllers/components—modules in Angular are slightly different from modules in AngularJS, and controllers are components in Angular.
Style sheets—more options for CSS than in AngularJS.
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 AngularJS template features with their equivalent Angular template syntax.
AngularJS | Angular |
---|---|
Bindings/interpolationIn AngularJS, 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, 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 AngularJS templates, use the pipe character (|) and one or more filters. This example filters the | PipesIn Angular 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 AngularJS are built-in pipes in Angular. For more information, see Filters/pipes below. |
Local variablesHere, | Input variablesAngular 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
AngularJS provides more than seventy built-in directives for templates. Many of them aren't needed in Angular because of its more capable and expressive binding system. The following are some of the key AngularJS built-in directives and their equivalents in Angular.
AngularJS | Angular |
---|---|
ng-appThe application startup process is called bootstrapping. Although you can bootstrap an AngularJS app in code,
many applications bootstrap declaratively with the | Bootstrappingmain.tsapp.module.tsAngular 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 AngularJS, the In the first example, the You can specify multiple classes, as shown in the second example. | ngClassIn Angular, the In the first example, the You can specify multiple classes, as shown in the second example. Angular 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 AngularJS, the In the first example, when the user clicks the button, the The second example demonstrates passing in the | Bind to the
|
ng-controllerIn AngularJS, the | Component decoratorIn Angular, 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 AngularJS, the | Bind to the
|
ng-hrefThe In AngularJS, the Routing is handled differently in Angular. | Bind to the
|
ng-ifIn AngularJS, the In this example, the | *ngIfThe In this example, the The (*) before |
ng-modelIn AngularJS, the | ngModelIn Angular, two-way binding is denoted by For more information on two-way binding with |
ng-repeatIn AngularJS, the In this example, the table row ( | *ngForThe Notice the other syntax differences:
The (*) before For more information, see Structural Directives. |
ng-showIn AngularJS, the In this example, the | Bind to the
|
ng-srcThe | Bind to the
|
ng-styleIn AngularJS, the In the example, the | ngStyleIn Angular, the In the first example, the Angular 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 the Style binding section of the Template Syntax page. For more information on the |
ng-switchIn AngularJS, the In this example, if | ngSwitchIn Angular, the In this example, if The (*) before For more information, see The NgSwitch directives section of the Template Syntax page. |
Filters/pipes
Angular pipes provide formatting and transformation for data in the template, similar to AngularJS filters. Many of the built-in filters in AngularJS have corresponding pipes in Angular. For more information on pipes, see Pipes.
AngularJS | Angular |
---|---|
currencyFormats a number as currency. | currencyThe Angular |
dateFormats a date to a string based on the requested format. | dateThe Angular |
filterSelects a subset of items from the defined collection, based on the filter criteria. | noneFor performance reasons, no comparable pipe exists in Angular. 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 |
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 |
numberFormats a number as text. | numberThe Angular Angular also has a |
orderByDisplays the collection in the order specified by the expression.
In this example, the movie title orders the | noneFor performance reasons, no comparable pipe exists in Angular. 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 AngularJS and Angular, Angular modules help you organize your application into cohesive blocks of functionality.
In AngularJS, you write the code that provides the model and the methods for the view in a controller. In Angular, you build a component.
Because much AngularJS code is in JavaScript, JavaScript code is shown in the AngularJS column. The Angular code is shown using TypeScript.
AngularJS | Angular |
---|---|
IIFEIn AngularJS, an immediately invoked function expression (or IIFE) around controller code keeps it out of the global namespace. | noneThis is a nonissue in Angular because ES 2015 modules handle the namespacing for you. For more information on modules, see the Modules section of the Architecture Overview. |
Angular modulesIn AngularJS, 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 modules, defined with the
For more information on modules, see Angular Modules (NgModule). |
Controller registrationAngularJS 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 adds a decorator to the component class to provide any required metadata.
The This is how you associate a template with logic, which is defined in the component class. For more information, see the Components section of the Architecture Overview page. |
Controller functionIn AngularJS, you write the code for the model and methods in a controller function. | Component classIn Angular, you create a component class. NOTE: If you are using TypeScript with AngularJS, you must use the For more information, see the Components section of the Architecture Overview page. |
Dependency injectionIn AngularJS, 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, 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 AngularJS, 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, you can still define style sheets for your entire application. But now you can also encapsulate a style sheet within a specific component.
AngularJS | Angular |
---|---|
Link tagAngularJS, uses a | Link tagIn Angular, you can continue to use the link tag to define the styles for your application in the StyleUrlsIn Angular, you can use the This allows you to set appropriate styles for individual components that won’t leak into other parts of the application. |